# Thread: [C++] TUT - Lesson 2 - Data Types & Variables

1. ## [C++] TUT - Lesson 2 - Data Types & Variables

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

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

Previous Lesson: http://www.codeleakers.com/showthrea...n-1-The-Basics
Next Lesson: http://www.codeleakers.com/showthrea...ring-Variables

0

2. Could you send me the link to the first tutorial :3

0

3. Originally Posted by sim0n-m0dz
Could you send me the link to the first tutorial :3
http://www.codeleakers.com/showthrea...-Prerequisites

Quotes removed from previous post.

0

4. since when is bool a type? is that just in C++? i mainly work in C :P

0

5. Originally Posted by demon450
since when is bool a type? is that just in C++? i mainly work in C :P
Yeah, there is not a boolean data type in C.

0

6. by the way long vars are the same as there type ie int = long int rarely need to use long's unless specifically asked too

0

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
All times are GMT -10. The time now is 02:32 AM.