Tie koodariksi

C++ Programming

Chapter 9: More on Types

Integers

C++ integer types are:
typeusual sizeusual lower limitusual upper limit
char8 bits–27 = –12827–1 = 127
short16 bits–215 = –32768215–1 = 32767
int32 bits–231 ≈ –2 · 109231–1 ≈ 2 · 109
long64 bits–263 ≈ –9 · 1018263–1 ≈ 9 · 1018
long long64 bits–263 ≈ –9 · 1018263–1 ≈ 9 · 1018
The C++ standard does not define precisely how big the types are - this depends on the environment used. Typical sizes are listed in this table for a modern computer.

A common choice for integer type is int, which is sufficient in most cases. If larger numbers are required, the type long (or long long) may be used.

Floating point numbers

C++ floating point types are:
typeusual sizeusual lower limitusual upper limit
float32 bits≈ –3.4 · 1038≈ 3.4 · 1038
double64 bits≈ –1.8 · 10308≈ 1.8 · 10308
long double128 bits≈ –1.2 · 104932≈ 1.2 · 104932
A floating point represents a decimal number. As with integers, the C++ standard does not define types exactly, and the table lists typical sizes today.

A common choice for floating point type is double. The following code shows two ways to use it:

double a = 5.72;
double b = 2e5;
In the first method, 5.72 has a decimal point, where it is interpreted as a floating point. Another way is to use e: for example, 2e5 corresponds to a floating point 200000.

Floating point numbers can represent very small and large numbers, but many numbers cannot be accurately represented this way, which can lead to rounding errors in calculations.

Type size

You can find out the size of a particular type in your environment with sizeof, which gives the size in bytes. For example, the following code prints the size of the int type:
cout << sizeof(int) << "\n";
The following output corresponds to a 32-bit type:
4

New name for a type

Use the word typedef to give a new name to a type. For example, the following line gives the type long long shorter alternative name ll.
typedef long long ll;
You can then use the new type name as follows:
ll x = 5;
Another newer way to do the same thing is to use using as follows:
using ll = long long;

Type conversions

C++ automatically changes type in different situations. For example, if you have operators of different types, the broader type is used. The following code illustrates this:
cout << 1/2 << "\n"; // 0
cout << 1.0/2 << "\n"; // 0.5
cout << 1/2.0 << "\n"; // 0.5
cout << 1.0/2.0 << "\n"; // 0.5
In the first calculation, both numbers are integers, so the calculation is done with integers. In other calculations, at least one number is a floating point, so the calculation happens with floating points.

You can also change the type by setting the desired type with parentheses before the value to be changed. You can put parentheses around the value or type you want to change:

cout << double(1)/2 << "\n"; // 0.5
cout << (double)1/2 << "\n"; // 0.5
This is known as changing cast or casting.

Overflow and underflow

If the integer value exceeds the maximum possible number or goes below the lowest possible number, the number rotates to the opposite side. For example, in the following code, the variable is maximum int value. When you add one, a variable will appear the smallest value of int .
int x = 2147483647;
x++;
cout << x << "\n"; // -2147483648 
Floating point numbers inf and -inf, indicate positive and negative "infinity". If the number becomes too large or too small, it becomes like this. For example, in the following code, the value of the variable exceeds double, so it becomes inf:
double x = 1e308;
x *= 2;
cout << x << "\n"; // inf

Non-existent numbers

Consider the following code:
int a, b;
cin >> a >> b;
cout << a/b << "\n";
If b is 0, the program ends in an error because dividing by zero is impossible:
Floating point exception (core dumped)
Then let's look at the corresponding code with floating points:
double a, b;
cin >> a >> b;
cout << a/b << "\n";
Now if b is 0, the program does not end in error, but the calculation will result in a special value. If a is positive, the result is inf, and if a is negative, the result is -inf. If a is 0, the result is -nan, which is the abbreviation for "not a number", that is, it is not a number (nor infinite).

Floating point accuracy

The weakness of floating points is that many numbers cannot be accurately represented as floating-point numbers and rounding errors occur. Let's look at the following code as an example:
double a = 0.3*3+0.1;
double b = 1;
cout << a << " " << b << "\n";
if (a == b) cout << "numbers are the same\n";
if (a < b) cout << "a is smaller\n";
if (a > b) cout << "a is greater\n";
The code produces a surprising result:
1 1
a is smaller
Although it looks like both variables have a value of 1, the variable a is slightly smaller. The reason is that 0.3*3+0.1 cannot be calculated accurately and will result in rounding error.

When using floating point numbers, take care that the results are not accurate. If there is no good reason to use floating point numbers, it is preferable to use integers.

Datatähti 2027 – peruskoulun ja lukion ohjelmointikilpailu käynnissä 26.10.–8.11.2026