WEBVTT

00:02.960 --> 00:07.000
Hello again! In this video, we're going to look at numeric types and literals.

00:09.760 --> 00:14.080
In C++, the size of the built in types isn't precisely defined.

00:14.470 --> 00:16.000
It depends on the implementation.

00:17.740 --> 00:22.750
The only one that is fixed is char, which has to be the byte size of the machine. And on anything that

00:22.750 --> 00:24.820
you likely to use, that's going to be 8 bits.

00:25.870 --> 00:30.550
An int has to be at least 16 bits. A long has to be at least 32 bits,

00:30.760 --> 00:37.150
and at least as many as int. And C++ 11 has a long long type, which has to be at least 64 bits,

00:37.750 --> 00:39.760
and it must be at least as many as long.

00:41.900 --> 00:48.830
If you want a fixed-width integer which has a well-defined size, then we have these now in the library.

00:49.010 --> 00:51.110
So you need to include the C standard int header.

00:51.680 --> 00:53.270
These have actually been brought in from C.

00:53.630 --> 00:57.620
So we have types for eight, 16, 32 and 64 bit ints.

00:57.950 --> 00:59.780
And also the unsigned versions.

01:04.720 --> 01:05.890
So let's see how these work.

01:10.940 --> 01:17.840
So there we are. char is one byte, which it is by definition. int is four bytes on this machine and long

01:17.840 --> 01:19.910
is also four bytes. Long

01:19.910 --> 01:23.810
long is eight bytes, which is 64 bits, so that's the minimum it could be.

01:24.530 --> 01:28.640
And then the fixed width types are what we would expect them to be.

01:35.670 --> 01:40.530
We can also use different bases in C++. If we don't say which base we want, then it's assumed we

01:40.530 --> 01:41.280
want decimal.

01:41.280 --> 01:44.700
So 42 is the number 42 in base.

01:44.700 --> 01:45.000
10.

01:46.110 --> 01:51.600
If we put 0x in front - it can be upper or lowercase - then that's interpreted as hexadecimal.

01:52.050 --> 01:56.460
So zero x 2 a that's 42 in base 16.

01:58.230 --> 02:01.380
There's also octal, which is base eight.

02:01.380 --> 02:05.490
So that has a leading zero. And there is actually a bit of a trap here.

02:05.790 --> 02:11.580
If you are dealing with strings that contain numbers, possibly with a GUI, then they may have leading

02:11.580 --> 02:12.120
zeroes.

02:12.480 --> 02:16.440
And in that case, when you convert them to integers, they are going to be interpreted as octal.

02:17.040 --> 02:21.760
So if you have something that starts with zero eight or zero nine, that's an error because octal only

02:21.760 --> 02:22.500
goes up to seven.

02:22.920 --> 02:27.810
And if it starts with zero one, zero two, zero seven, then you just get the wrong number.

02:29.520 --> 02:30.960
So that's something to watch out for.

02:31.080 --> 02:32.550
I have actually been caught by that.

02:34.870 --> 02:41.920
And finally, in C++ 14, we can also have binary literals with zero b and that's 42 in

02:41.920 --> 02:42.100
binary.

02:43.390 --> 02:44.890
So let's just try that out.

02:45.310 --> 02:49.780
So there we are. OK, they all give 42 in different bases.

02:55.040 --> 02:59.570
Floating points. Again, these aren't precisely defined, but you can be fairly certain what you get

02:59.570 --> 03:05.060
on current hardware. With floats, you will usually get six digits precision, so that's all the digits

03:05.060 --> 03:05.600
in the number.

03:06.140 --> 03:12.710
Regardless of whether they are before or after the decimal point. With double, we get 15 digits and long

03:12.710 --> 03:16.640
double is 20, which came in in C++ 98, I think.

03:19.850 --> 03:25.310
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

03:25.310 --> 03:29.950
just the quote character, so we can write one million with the quote inside it.

03:30.380 --> 03:32.450
And this came in in C++ 14.

03:33.560 --> 03:35.120
And it works with decimals as well.

03:38.510 --> 03:43.550
Normally, you'll probably use the Western convention, in which there's a separator after every three digits,

03:43.550 --> 03:47.390
but there are other conventions. And you can in fact put the separator anywhere you want.

03:47.780 --> 03:53.630
So if for example you want to use the Indian convention, then you can just put the separator in the appropriate

03:53.630 --> 03:54.020
place.

03:55.040 --> 03:59.810
The separator has to go inside the number. It can't be at the beginning or the end as otherwise that will

03:59.810 --> 04:01.760
be interpreted as a character literal.

04:05.310 --> 04:07.890
OK, so here's some code, so we can just try this out.

04:07.920 --> 04:13.980
We have one million, one lakh and we have pi, using these separators, and then we're going to print them out.

04:14.870 --> 04:17.790
And yep, so that's one million.

04:17.970 --> 04:23.130
It's quite hard to tell just from looking at that, that is just one million and not 100,000.

04:23.940 --> 04:27.090
So writing them out like that in source code can make the numbers clearer.

04:32.780 --> 04:36.890
Literals have a default type, if we don't see what type they are.

04:37.070 --> 04:39.110
A floating point literal will be double.

04:40.820 --> 04:42.590
If we have an integer, it'll be int.

04:42.650 --> 04:44.840
Unless it's too big, in which case it'll be a long.

04:45.380 --> 04:47.570
And if it's too big for long, then it'll be a long long.

04:48.320 --> 04:51.050
And if it's too big for long, long, then you have an overflow.

04:52.930 --> 04:58.510
If you don't want to use these default types, you can add a suffix. So if you want three point

04:58.510 --> 05:01.640
one four one five nine to be a float, then you put an 'f' after it.

05:02.470 --> 05:05.350
That can be upper or lowercase. And there are others.

05:05.530 --> 05:08.650
So, for instance, you can make this number an unsigned long long.

05:11.650 --> 05:18.250
So you can think of this letter 'f' or 'ULL' as a kind of operator, which returns an object of this type.

05:18.730 --> 05:25.330
And in fact, in C++ 11, you can provide your own suffixes which work with your own types and they'll

05:25.360 --> 05:26.590
return an object of that type.

05:29.710 --> 05:31.480
OK, so that's it for this video.

05:31.810 --> 05:34.900
I'll see you next time, but meanwhile, keep coding!
