noobtuts

C++ Tutorial for noobs - Part 3: Data-types

...Back to Part 2: Output

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:
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:

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 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:

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.

Continue to Part 4: Variables...