Hello again! In this video, we're going to look at numeric types and literals.
In C++, the size of the built in types isn't precisely defined.
It depends on the implementation.
The only one that is fixed is char, which has to be the byte size of the machine. And on anything that
you likely to use, that's going to be 8 bits.
An int has to be at least 16 bits. A long has to be at least 32 bits,
and at least as many as int. And C++ 11 has a long long type, which has to be at least 64 bits,
and it must be at least as many as long.
If you want a fixed-width integer which has a well-defined size, then we have these now in the library.
So you need to include the C standard int header.
These have actually been brought in from C.
So we have types for eight, 16, 32 and 64 bit ints.
And also the unsigned versions.
So let's see how these work.
So there we are. char is one byte, which it is by definition. int is four bytes on this machine and long
is also four bytes. Long
long is eight bytes, which is 64 bits, so that's the minimum it could be.
And then the fixed width types are what we would expect them to be.
We can also use different bases in C++. If we don't say which base we want, then it's assumed we
want decimal.
So 42 is the number 42 in base.
10.
If we put 0x in front - it can be upper or lowercase - then that's interpreted as hexadecimal.
So zero x 2 a that's 42 in base 16.
There's also octal, which is base eight.
So that has a leading zero. And there is actually a bit of a trap here.
If you are dealing with strings that contain numbers, possibly with a GUI, then they may have leading
zeroes.
And in that case, when you convert them to integers, they are going to be interpreted as octal.
So if you have something that starts with zero eight or zero nine, that's an error because octal only
goes up to seven.
And if it starts with zero one, zero two, zero seven, then you just get the wrong number.
So that's something to watch out for.
I have actually been caught by that.
And finally, in C++ 14, we can also have binary literals with zero b and that's 42 in
binary.
So let's just try that out.
So there we are. OK, they all give 42 in different bases.
Floating points. Again, these aren't precisely defined, but you can be fairly certain what you get
on current hardware. With floats, you will usually get six digits precision, so that's all the digits
in the number.
Regardless of whether they are before or after the decimal point. With double, we get 15 digits and long
double is 20, which came in in C++ 98, I think.
If you have a number with a lot of digits in, you can put a separator in to make it easier to read. That's
just the quote character, so we can write one million with the quote inside it.
And this came in in C++ 14.
And it works with decimals as well.
Normally, you'll probably use the Western convention, in which there's a separator after every three digits,
but there are other conventions. And you can in fact put the separator anywhere you want.
So if for example you want to use the Indian convention, then you can just put the separator in the appropriate
place.
The separator has to go inside the number. It can't be at the beginning or the end as otherwise that will
be interpreted as a character literal.
OK, so here's some code, so we can just try this out.
We have one million, one lakh and we have pi, using these separators, and then we're going to print them out.
And yep, so that's one million.
It's quite hard to tell just from looking at that, that is just one million and not 100,000.
So writing them out like that in source code can make the numbers clearer.
Literals have a default type, if we don't see what type they are.
A floating point literal will be double.
If we have an integer, it'll be int.
Unless it's too big, in which case it'll be a long.
And if it's too big for long, then it'll be a long long.
And if it's too big for long, long, then you have an overflow.
If you don't want to use these default types, you can add a suffix. So if you want three point
one four one five nine to be a float, then you put an 'f' after it.
That can be upper or lowercase. And there are others.
So, for instance, you can make this number an unsigned long long.
So you can think of this letter 'f' or 'ULL' as a kind of operator, which returns an object of this type.
And in fact, in C++ 11, you can provide your own suffixes which work with your own types and they'll
return an object of that type.
OK, so that's it for this video.
I'll see you next time, but meanwhile, keep coding!