In this video, we'll learn about some of the commonly used c++
primitive data types.
These types are also sometimes called fundamental data types because they're implemented
directly by the c++ language.
The types we'll discuss in this video are the character types,
the integer types which include both signed and unsigned integers,
the floating point types and the Boolean type.
It's important to keep in mind that unlike many other programming languages,
the size and precision of c++'s primitive data types
are largely dependent on the platform that you're working on in the compiler you're using.
This means that as a c++ programmer,
you need to be aware of your specific machine and understand how much storage
is allocated for these types to effectively use them.
The c++ include file climits
contains information about the size and precision
of the data types for your specific compiler.
As you know, computers store information using a binary representation,
which consists of zeros and ones.
And these fundamental type sizes are expressed in bits.
The more bits allocated to a type, the more unique values that can be represented.
Also the more bits, the more storage that's necessary
to represent that type in memory.
In this table, you can see how many values can be represented in a given number of bits.
The formula used to calculate these values is 2,
raised to the number of bits power.
So we can store 256 distinct values in 8 bits.
65,536 distinct values in 16 bits.
Over 4 billion distinct values in 32 bits.
And over 18 billion, billion distinct values in 32 bits.
Now let's take a look at the specific primitive types one at a time.
The c++ character data type is used to represent characters
such as the letter a, x and so forth.
The character data type is often implemented as eight bits,
which means it can represent a maximum of 256 characters.
This is plenty to represent the alphabets for many spoken languages.
However, some spoken languages have thousands of characters.
And in many cases, multiple symbols are combined to form single characters.
In order to support these languages, c++ supports a wider character type
that can be as large as necessary to represent these characters.
Unicode is a common standard used to represent multiple character sets for any language.
When we work with characters in this course,
we'll use the char data type since it easily supports the Latin character set.
C++ also supports the integer data types.
Integers are used to represent whole numbers,
both signed and unsigned integers are supported,
there are many variants of the integer data type, let's take a look at some of them.
The following tables show the c++ integer data types for
both signed and unsigned integers.
In addition to those shown in the table,
it's possible to store both signed and unsigned integers in the character data type.
Notice that some of the keywords in this table are bold and others are italicized.
Also typical sizes are included in the table.
For example, if you want to declare a signed integer
you don't need to use the signed keyword
since by default integers are signed.
Similarly, if you want an integer type that stores very large numbers,
you can simply declare the type as long, long.
And you'll get a signed long, long integer.
If you want unsigned integers that is integers that are 0
or positive values only, then you're required to use the unsigned keyword.
I know this may seem a little confusing with all the optional keywords,
but it's not so bad if you just think that by default all integers are signed.
I know what you're thinking. Which ones of these do i use in my application?
And the answer is it depends.
If you want an integer variable, it stores ages
then you could use an unsigned int or even an int.
If you want an integer to represent the number of kilometers between the planets,
then you probably want to use an unsigned long, long.
For most applications, using int is perfectly acceptable.
But you do need to keep in mind the limitations especially when you perform
math on variables and store results in other variables
since the results may not fit into the variable you're saving to.
This could cause an overflow.
We'll look at a specific example of an overflow and live code at the end of this video.
C++ also supports floating point types.
These are used to represent non-integer numbers, such as real numbers,
numbers like 1.2, 0.5 and pi.
It's important to understand that computers have finite storage
and many real numbers have an infinite number of digits after the decimal point.
Pi is a perfect example of this.
In these cases, computers store an approximation of the real number.
There's no way the computer can store numbers such as pi precisely.
Floating point numbers are represented internally by the computer as signed numbers
with a mantissa and an exponent.
You may remember your scientific notation from school,
1.234 times 10 to the seventh power.
That's the way they're stored internally.
This isn't something you need to worry about since it's handled automatically internally.
But it's important to know that these are approximations.
There are three types of floating point numbers. Float, double and long double.
Each has varying precision and size depending on the compiler.
Note that long double can store very, very small
and very, very large floating point numbers.
The last primitive we'll learn about is the Boolean type.
The Boolean data type is used to represent true and false values.
In c++, 0 is false
and any non-zero value is true.
C++ also includes the true and false keywords
that are often used with the Boolean type.
As you can imagine, Boolean types are used whenever you need to know if something changes state.
For example, game over, key pressed, is invincible.
These are all good examples of using the Boolean data type
since their values can be either true or false.
Great. Now let's head over to code line and see some of these primitive types in action.
So now I'm in the CodeLite IDE,
and I've created a project called primitive types that's in the section 6 workspace.
In this c++ file,
I'm going to go over some examples of using the primitive types.
We'll start with characters. We'll do integers and floating point numbers and then at the end, we'll do Booleans.
And I'll also show you an example of an overflow so you can see
the typical behavior you get with those.
I've written the code ahead of time, and I've commented out a lot of it.
And what I'll do is I'll uncomment and run pieces of it at a time,
so you can see it build.
So the first thing we want to do is take a look at this first example here.
This is a good example of using the character data type.
My middle initial is a J. So the variable name is called middle initial,
and it's of character data type. So it can hold a single character.
The way that c++ represents single characters
is with single quotes around the J do.
Not put double quotes there that would make it a string and you'll get an error.
So what I'm doing here is I'm simply
initializing the middle initial variable to J
and then displaying it right here in the output statement.
So I'll build and run
and I'm just going to click here remember so it doesn't ask me
my choice all the time, and I just want to build and execute.
You can see here it says my middle initial is J. Exactly what I expect.
Okay. So now let me uncomment the integer types here.
The way that you can comment and uncomment lines very easily is just select the lines you're interested
in and just press ctrl / or command / on a mac,
and that will toggle between a comment and non-comment.
Okay. So in this case,
I've created a variable called the exam score,
and I'm initializing it to 55.
Now exam score is an unsigned short int.
Remember, we have to say unsigned if we want our variables to be unsigned.
But i don't have to say int, I can simply say short.
So this is the same as saying unsigned short exam score.
Okay. So I'm initializing that to 55. And again I'll build and run.
And now you can see my middle initial still J, and my exam score was 55.
Okay. In this case, we've got another number,
and I'll uncomment that out. The number of countries represented in my meeting
were 65,
and I'm initializing it. This is a simple integer by default. It's going to be signed.
And here it's going to say there were 65 countries represented in my meeting.
So let me run that again. I'm pressing ctrl F5 on windows
just to use the shortcut.
And you can see here there were 65 countries represented in my meeting.
Now in this case, we've got a much larger number.
Let me uncomment these two lines out.
So now I've got people in Florida. That's my variable name.
This is the state I live in, and there are 20,610,000 people
living in Florida the last i just checked on google.
So I'm defining this as a long.
I'm hoping that, that fits in the in the long. It's a pretty big number.
I'm pretty sure it does. Let's try that.
So I'll press ctrl F5 again, we'll run it, and you can see there are about
20,610,000 people in Florida.
Now in this case, how about number of people on earth.
I'll uncomment those two lines. And you can see my variable name is people on earth
and there are about 7,600,000,000 people on earth.
Now something that's interesting is as these numbers get bigger and bigger and bigger.
They're hard to read and. It's really easy to forget zeros and so forth.
So the c++14 standard
allows you to use those little tick marks right there. You can put them anywhere you want. It really doesn't care.
Just to make it easier for you to read it strips them out it just creates the number.
If this doesn't compile on your end
most likely you're not using the c++
14 compiler. You're using the c++11 compiler.
You could set it to c++14 or you could just get rid of these little tick marks
to make it compile. So when I run this, you'll see something interesting happen.
I'm pressing ctrl F5 again.
Notice I'm getting an error. The error says, error narrowing conversion.
It's trying to fit that number into a long, and it doesn't
fit.
This is one of the benefits that I mentioned about using the list initialization
right here with the curly braces.
Notice what happens if I did but actually you know what let me just run this first
as a long, long which is really what I want because otherwise it's not going to fit.
So now I've got my long, long. And I'm going to build and run this.
And you can see there are about seven point. You can see the number is correct.
Okay.
But if I did a regular initialization
like a c style initialization with an equal sign here or an assignment statement
something like that.
And we'll go back to long because we know it's not going to fit in along.
And I'll build and run again, look what prints out.
There are about -98999 people on earth.
That's an example of an overflow.
But you also notice that we didn't get an error.
That's one of the benefits of using the list initialization.
If I use listeners initialization, it will catch those problems for me
at compile time, which is really, really nice.
So again, I'll compile here.
Oh ctrl F5, boom there's the error. I know I've got a problem. I can fix it.
So I'll make this a long, long.
Okay. So hopefully that makes sense. Always use that list initialization syntax. It's so much better.
In this case, I want the distance to alpha Centauri. That's pretty far. This is,
I believe, in kilometers, yep. It's in kilometers. That's what my output statement says.
I'll uncomment these two lines. And let's run this.
And you can see that the distance to Alpha Centauri is. And that's my number. That's correct.
Okay. So that's a good example of an overflow. I'll show you another example of an overflow
at the end of this source code that may not be as obvious as this.
Okay. So here's a couple of examples using floating point types.
Remember, these are real numbers. These are numbers with decimal points.
So in this case, my car payment is $401.23.
I can uncomment this out.
Obviously, if I try to store $401.23 into an integer,
I've got a problem because the integer would only hold the whole number.
So the $0.23 would get truncated off and i could have a problem. Okay.
So in this case. Let's run this.
And now you can see that my car payment here is $401.23.
Okay. If I want to store a larger number, I could use a double.
In this case, I'm just storing pi.
And this guy, I'm hard coding pi to be 3.14159.
It could be obviously much, much longer. And in this case, it's a double.
And you can see that there's pi 3.14159.
I can also store very large and very small numbers using long doubles.
So in this case, I'm storing 2.7 times 10 to the 120th power.
That's a very large number. And I'm going to store that into this large amount variable,
and then I'm going to display it. So let's build and run.
And you can see 2.7
times 10 to the 120th power is a very big number.
You can see it's actually storing that correctly.
The last one we'll look at is the Boolean type. The Boolean type is a true false value.
So in this case,
I'll comment that out. You can see I've got a variable called game over.
Let me use the correct_style here.
I've got a variable called game over/and in this case,
you could see that I've been working on a project that's using camel case,
muscle memory kicks in. So in this case, I've got a variable
called game over,
and I'm initializing it to false.
And then I'm saying the value of game over is,
and you might expect that this would print out faults here
but it doesn't print up vaults. What it does is it prints out zero.
Because as far as c++ is concerned zero is false.
If it was true, it would print out a one.
Now here's an example of an overflow that's not so obvious,
and I'll comment that out.
Here, I'm so -- I have a variable called value one,
and I've got another variable called value two. They're both short integers.
And I'm assigning 30000 to this one and 1000 to this one.
Those numbers will fit in those variables.
But then I'm going to create another variable called sum,
and I've got value one times value two.
Now I know that this the product of those I guess I should have called it product huh,
let me call it product just to be
a little bit more correct here,
there we go.
So I know that the product of those two numbers
does not fit in a short. So let's compile this,
and you can see that the sum of 30000 and 1000 is some
negative number.
Obviously, that's an overflow error.
Be very careful with these things. Make
sure those two pieces each will individually fit in the shorts but the product certainly can't.
Okay. But notice the compiler is helping me. Here the compiler comparison warning.
You've got potentially a narrowing conversion here.
So don't ignore those warnings as the compilers do a pretty good job about
predicting problems that can happen.
Okay. So that concludes this video. You can see some simple examples of
creating variables, initializing variables,
using some of c++ primitive types.