C++ Tutorial for noobs - Part 3: Data-types
Theory
Alright, let's start with some theory.
Uaaaaaah no theory nooo!!
Don't worry, it's easy and it will only take five minutes or so. All we have to know is that everything in C++ has a type.
Imagine a bottle. If that's too hard, here is a picture of a bottle:
Now in reality, a bottle can contain just about anything: water, beer, sand or whatever. In some programming languages this is the case too, where things can have just about any type. But in C++ it's (for some complicated computer science reason) different. C++ wants us to specify what type of bottle we have: is it a water bottle (that can contain all different kinds of water), or is it a beer bottle (that can contain all kinds of different beer flavors) and so on.
That's all there is to know about data-types theory.
Basic Data-Types
Alright, so there are a lot of different data-types in C++, here are the most basic ones that everyone has to know:
- bool (also known as boolean)
- something that is either True or False
- int (also known as integer)
- a number like -1, 0, 1, 2, 3, 4, ...
- float (also known as floating-point)
- a number like 1.23456
- string
- a bunch of characters like "abcd" or "abc 123"
Combining the same Data-Types
Okay so we don't like math, so we use C++ to find out what 1 + 2 is. If we look at the list of data-types above, we can see that 1 and 2 are both values of type int. And if we combine two int values, that makes the result an int value again (3 is obviously int).
Generally speaking: if we do something with two data-types of type 'WHATEVER', then the result is usually the data-type 'WHATEVER' again.
Combining different Data-Types
Now the question is: what happens if we use C++ to calculate 1 + 0.5 where 1 is a int value and 0.5 is obviously a float value?
There are only two options:
- 1 + 0.5 becomes a new int value
- 1 + 0.5 becomes a new float value
1 + 0.5 results in 1.5, which is a a float value. Hence we need a float value.
That's not that hard right?
Advanced Data-Types
Actually there are few more data-types than in the list above. If you are overwhelmed with the previous ones, then don't worry about it too much. If you are ready for some more knowledge, then here is a more complete list:
- bool
- something that is either True or False
- char (also known as character)
- a single character like 'a', 'b' or 'c'
- wchar_t (also known as wide-character)
- a single (weird) character like '♣' '§' '®'
- short
- a very small number like -1, 0, 1, 2, 3, 4
- can be between –32,768 and 32,767
- unsigned short
- same as short, but only the positive ones (0, 1, 2, 3, ...)
- can be between 0 and 65,535
- int
- a number like -1, 0, 1, 2, 3, 4, ...
- can be between –2,147,483,648 and 2,147,483,647
- unsigned int
- same as int, but only the positive ones (0, 1, 2, 3, ...)
- can be between 0 and 4,294,967,295
- float
- a number like 1.23456
- can be up to 7 digits
- double
- same as float, but with more precision
- can be up to 15 digits
- void
- the none-type, more about that in the functions tutorial
We just saw four kinds of data-types for numbers like 1, 2, 3, ..., which is because of limitations in our computer's memory. Obviously its not infinitely huge, that's why there are data-types for 'small numbers', 'bigger numbers' and so on.
The f-Suffix
One more thing to keep in mind: in C++ we will see a lot of values like:
0.123f
which shouldn't confuse us. The reason for the f at the end is to let C++ know that it's a float and not a double value.
Final Hint
If those data types really freak you out, then just go with float for all numbers, string for all "words" and you are fine for now.