Join Bill Weinman for an in-depth discussion in this video Integral types, part of C++ Essential Training.
- The Integer types are simple, fundamental data types for representing Integer values, that is whole numbers with no fractual part. The Integer types include "char", c-h-a-r, I pronounce it "char", some people say "char". "int", "short int", "long int" and "long long int". These are all available in both signed and unsigned versions. The size of each type vary depending on both the architecture and the compiler. The "char" type is defined as the minimum size necessary to contain a character.
On most modern systems that's eight bits. The "char" type may be signed or unsigned by default, that is on some systems it may be signed and on other systems it may be unsigned, you cannot count on a "char" being one or the other, the only thing you can count on is that it will hold a character. All the other Integer types are signed unless modified with the "unsigned" keyword. The "short int" is the smallest natural size of an Integer for the target processor. It may be the same as "int", on most modern desktop systems it is 16 bits.
The "int" type is the natural size of an Integer for the target processor. On most modern systems it's 32 bits. The "long int" type is at least the size of an "int", and it's often double the size of an "int". On most modern systems it's either 32 or 64 bits. The "long long int" is at least the size of a "long int", it may be double the size of a "long int". On most modern systems it's 64 bits. For the signed types, the most significant bit is a sign bit.
Unsigned types are available with the "unsigned" modifier. If your usage requires an Integer of a specific type, the "cstdint" header provides types in these specific widths, eight bits, 16 bits, 32 bits and 64 bits. And these are available both in signed and unsigned varieties. Here I have a working copy of "working.cpp" from chapter five of the exercise files. This system is a Mac with an Intel 64-bit processor using the LLVM Clang compiler, and so let's look at some of the sizes of the Integer types.
We do this like this, I'm gonna say "printf('sizeof char is %ld bits')", and I'm using the "ld" here because that's the "long" Integer, and "sizeof" returns "long integer", and I'm gonna use "sizeof" right here. And so that'll print out the size of a "char", and I'm actually multiply that by eight so we get it in bits. And when I build and run you see it says, "sizeof char is 8 bits". So I'm gonna go ahead and copy and paste this five times, and get one more, and we'll say "short int", regular "int", and then "long int", and "long long int", and then do the same over here.
And this will just tell us what sizes all of these are on this system. So now when I build and run we see that a "char" is eight bits, the "short int" is 16, a regular "int" is 32, a "long int" is 64, and a "long long int" is also 64 bits. The "sizeof" operator returns the number of bytes for a given type, or the resulting expression here, I multiply the result by eight to get the result in bits. On a PC we'll see some slightly different results so we'll get to that in a moment.
It's worth mentioning that due to a legacy feature from C, "int" is a sort of a default type, and what that means is if I take out the word "int" here and just leave "short", that'll actually give me exactly the same result here, 16. So "short" is just a shorthand for "short int". And it's also true of "long" and of "long long". And so if I build and run you see all of those work exactly the same. For my part I don't like using the shortcut, I think it's a bad idea, I suggest you always use the full type name, but you need to know that this capability is here, because you will see it sometimes in existing code.
So, now I've done something a little bit different here in this movie, I actually also have Windows running on the same machine, I'm using Parallels, and it's Windows 8.1, and I'm running Microsoft Visual C++, Visual Studio, and I have exactly the same file open. So if I switch over to Windows, which I'm gonna do right now. And you see that it's got a little dialog box here saying that the file was changed, I'll just say Yes, and that's because this has exactly the same file open that is open on the Mac side of this same machine, so they work on exactly the same file.
And you see that I've got this with all of these types in it here. I'm gonna press F7 on this Windows machine, and I'm going to... That'll build this, and then I'm going to switch to a Command Line, where I can run it, and you see our results over here. So you notice that it's slightly different. On the Mac "long int" was 64 bits, and here it's 32 bits. Yet "long long int" is still 64 bits on both machines, and regular "int" is still 32 bits on both machines. The only difference here is that "long int" is 32 bits on the PC.
And so that can actually create a problem if you're trying to run the same code in both environments, and if you're counting on a particular variable being a certain number of bits, a certain size. And so for that reason we actually have the C Standard Int types, and those are available in this header called "cstdint", like that. And so, instead of these types here, I'm just going to change this, I'm gonna say "int8_t", and it looks like that, and I'll go ahead and copy and paste that.
And I'm just going to take this and copy and paste it four times so that we can see the different sizes. So, we also have 16, and 32, and 64-bit sizes of these, and I'll change them on this side also. And now I'm just gonna go ahead here on this PC and I'm going to build, and we'll look at it in our Command Line window, and you see that we can now be sure that we have eight, 16, 32 and 64 bits.
And if I switch back, you see I have exactly the same file open on both of these, and so I'm just gonna run Clean, so it doesn't get confused, and I'll build and run, and you see that we have exactly the same result here, eight bits, 16 bits, 32 bits and 64 bits. So if you really are concerned with the size of your Integers, and specially if you're going to be concerned with it cross-platforms, then you're gonna wanna use the "cstdint" versions rather than the "char", "int", "short int", "long int". Just one more quick thing we wanna learn about what we're talking about, Integer types, and that's Literal values.
So I'm just going to declare a "long" Integer here, and I'm just gonna call it "x" and I'm gonna give it a value, and I'm gonna go ahead and print it out here. And that's actually a "long" so I'm going to give it an "l" there, and... Oh, I give it a semicolon. There we go. Now when I build and run you see it says, "x is 42". So, this 42 is actually a "decimal" Integer. If I want to specify that it's a "long" Integer I can put an "L" after, like that.
Or if I wanna specify that it's a "long long" Integer I can give it two "L"s. If I wanna specify that it's unsigned I can give it a "U". And I can also, if I want to, I can specify it in octal. I can say "0223", and when I build and run that you notice that the result is 147, that's because this is an octal number. When you use a leading zero on a number then C will interpret that as octal, base-8. So you have the digits available zero through seven, if I try and put an eight in here it'll actually give me an error because it's an invalid digit in octal.
I can specify in hexadecimal, I can say "0x93", which also happens to be 147. And of course in hexadecimal you also have A, B, C, D, E and F. So if I say, "0xFF93" I get that big number. And in C++ 11 there is no standard for binary Literals, in C++ 14 there is, and this particular compiler is already implementing it. I can say, "0b10010011", like that, and when I build and run you see that the value is, again, 147.
And that's also the name of a Star Trek episode. Unfortunately, this is from a later standard, the C++ 14 standard, and this will not work on a Windows compiler. So if you're trying to compile this, at least today, as I'm recording this at the end of 2014, this will not work in the version 18, version of the Microsoft C++ compiler, in later versions I expect that it will work. So, C++ provides a full selection of Integral types, in many sizes, both signed and unsigned.
- What is C++?
- Anatomy of a C++ program
- Writing statements and expressions
- Declaring variables
- Using loops
- Defining functions
- Getting the most out of the preprocessor
- Creating classes and objects
- Undertanding data types
- Overloading operators
- Understanding inheritance
- C++ template programming and the STL
- Handling exceptions