• IMPORTANT: Welcome to the re-opening of GameRebels! We are excited to be back and hope everyone has had a great time away. Everyone is welcome!

C++ Lesson 2|Data types and variables

Fuzed

Well-Known Member
Joined
May 19, 2012
Messages
1,452
Reaction score
2
Ok so now we have learned to code C++ and made our first Hello World app we can move on.

Let's move on to variables and data types.

Variables
Variables are letters or words or whatever else you define them as within your code (we shall get onto declaring variables in a minute). The word you use for the variable is called an indentifier. So for example you could have some code such as this
Code:
a = 9;
b = 5;
a = a + 1;
result = a - b;

So you have 2 variables in this code a and b. You assign the value of 9 to a, you assign 5 to b, then a increases by 1. The result (another identifier) is then (a+1)-(b) = (9+1)-(5) = 10-5 = 5. So the result is assigned 5.

Do remember though:
- identifiers (a, b and result) cannot be completely anything - you cannot use reserved words
asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete,
do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto,
if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register,
reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template,
this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void,
volatile, wchar_t, while


- C++ is case sensetive so if you use an identifier such as 'a' then do not then start using 'A' as the program deals with cases as if they were sepearte letters/words.

-------------------------------------------------------------------------------------------------
Data Types
To declare our variable we need to decide what data type it is, here is a table to help you:
Name - Description- Size* - Range*
char - Character or small integer. - 1byte - signed: -128 to 127 unsigned: 0 to 255
short int (short) - Short Integer. - 2bytes - signed: -32768 to 32767 unsigned: 0 to 65535
int - Integer. - 4bytes - signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295
long int (long) - Long integer. - 4bytes - signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295
bool - Boolean value. It can take one of two values: true or false. - 1byte - true or false
float - Floating point number. - 4bytes - +/- 3.4e +/- 38 (~7 digits)
double - Double precision floating point number. - 8bytes - +/- 1.7e +/- 308 (~15 digits)
long double - Long double precision floating point number. - 8bytes - +/- 1.7e +/- 308 (~15 digits)
wchar_t - Wide character. - 2 or 4 bytes - 1 wide character

* The values of the columns Size and Range depend on the system the program is compiled for.

The memory in our computer is organised in bytes. A byte is the minimum amount of memory that we can
manage in C++, it can store a relatively small amount of data: a single character or a small integer
(generally an integer between 0 and 255). However we can use more bytes and store more complex/longer/bigger numbers.
 
Top