Variable Types in C++ Bools, Floats, Integers, Arrays, Characters and Strings
C++ comes with many variable types. These variables store different types of information that allows you to perform different operations to yield desired results. The variable types I will be introducing are the integer or int, the float, characters, bools, arrays and strings.
Integers, int in c++, are variable types that store numbers such as 20, 15 or 100. Numbers that contain decimals such as 15.5 are not integers because they contain decimal points. There is a limit to what an integer variable type can store. Typical machines use 32 bits, or 4 bytes, to store an integer while others have 64 bit integers. The range for a 32 bit integer is 2,147,483,647 or (231-1) to -2,147,483,648 or (-231) for signed integers. For unsigned integers the value is 4,294,967,295 or (232-1). 64 bit versions are -9,223,372,036,854,775,808 or (-263) to 9,223,372,036,854,775,807, or (263-1)
Generally an integer is declared like so:
int myInt; myInt = 75;
Floating Point Numbers
Foating point numbers, or in c++, float, are numbers that have a decimal point contained within them. Numbers such as 20.0 are floating point numbers while numbers such as 20 are not because they do not contain decimal points. Floating point numbers can be expressed expenentionally for instance 1.2e19 is 1.2x1019. There is a limit on the range that is able to be stored in a float variable and varies widely depending on the machine. Here is an excellent page on floats
Floats can be declared like such:
float myFloat; myFloat = 33.9;
Boolean types are variables that simply hold a true or a false and can be declared as such:
bool skyIsBlue; skyIsBlue = true;
Characters are used to store single characters such as a, b, or c. Characters are declared with single quotations such as
char myChar = 'a';
There are special characters called escape characters that begin with a \ and followed by a character. These special characters perform special actions. A table of escape characters can be found here.
Characters were designed to hold only the basic character set for american characters. For foreign languages wide characters were created. Wide characters use the pefix L to indicate it is a wide character. Characters can be initialized in the following manner:
char variableName; variableName = 'a';
Wide characters can be initilaized in the following way:
wchar_t wide; wide = L 'ÃÂ©';
The symbol represents a character in another language.
Arrays are special storage containers in c++ that allow you to store data in consecutive memory locations. Items in arrays are called elements and the number of elements in the array is called the dimension of the array.
Arrays can be declared like this:
This declares the array to be an array of 10 elements, myArray, myArray, myArray.... and so on until you get to myArray. Notice how the array starts at zero instead of 1 and goes to 9. The number inside of the bracket is called a subscript. When accessing a value inside of an array you use the subscript to get the data stored in that particular container. If you were to try to access anything in the array past 9 i.e. myArray you will get an error and possibly crash your program because 10 will be the 11th element in the array since arrays start at 0.
Arrays can be declared like in the following manner:
int myArray; myArray = 1; myArray = 2;
The elements of the array are initialized to the values given. The first element of myArray is equal to 1 while the second element is equal to 2.
Strings are just like an array of characters meaning they are characters stored in consecutive memory locations. You can use strings when you want to store words in a variable. Strings are not built into the c++ core language so in order to use them you have to first use an include statement so they can be included form the c++ library.
Then you can declare a string in this way:
string myString; myString = "cupcake";
As you can see strings have many different characters contained in succession, hence the name string. Since a string is an array of characters an element in a string can be accessed in the following manner:
cout << myString << endl;
This will output the character c to the sceen since you are accessing the 4th element in the array of characters or string. Remember 3 is the 4th element since arrays start at 0. Just like characters there are also wide strings for use with foreign languages. Wide strings can be declared this way:
wstring myName; name = L "ÃÃÂ¯ÃÂ²";
Wide strings begin with the letter L just as with wide characters. The symbols represent the characters in another language. There are many other operations that can be performed on strings but that is for another hub.
In this hub I have went over a few of the variable types in C++ and how to declare and initialize them. There are integers and floats that store numeric values, there are characters and strings used to store single letters and chains of letters respectively. Arrays are like containers that store variable consecutivley and bools hold true or false values. These are just a few types of variables in c++ but hopefully this helps anyone reading this get started on their way to becoming a programmer.