Variables
Data is stored in the memory.
However, if I want to store certain data in a data block, I'll need to name it so I can call it specifically. This is a variable.
So, a Variable is a named memory location.
Rules to Naming Variables
- Has to start with a letter or underscore (preferably don't start with underscore)
- After the first letter, you can use letters, numbers or underscores.
- No spaces.
- No keywords.
C++ variables are case sensitive, so age, Age and AGE are different.
DataTypes
Now, in this variable the kind of data we store is called a Datatype.
So, a DataType helps us specify the size and the type of data.
Types of DataTypes
There are 3 categories of datatypes:
- Built In / Primitive Datatypes
- Derived Datatypes (To be learnt in later stages)
- User Defined Datatypes (To be learnt in later stages)
Primitive Datatypes
The basic datatypes in use are:
1. int
Stores integer numbers.
Syntax as:
int num = 123;
2. float
Stores decimal numbers.
Needs to be suffixed with a f since c++ automatically treats it as a double.
Syntax as:
float num = 12.34f;
3. double
Stores higher precision decimal numbers.
Syntax as:
double num = 12.34567;
4. char
Stores characters.
Needs to be enclosed in single inverted commas ''.
Syntax as:
char ch = 'a';
5. bool
Stores true or false values.
Similarly, also a 1 represents true and 0 represents a false.
Syntax as:
bool isHere = false;
isHere = 0;
isHere = true;
isHere = 1;
6. void
This will be discussed with functions for more understanding.
Variation of int and double
There are further variations to integers as:
| Type | What it means |
|---|---|
short |
Short integer |
int |
Regular integer |
long |
Long integer |
long long |
Longer integer (suffix LL) |
unsigned int |
Integer that cannot be negative |
| Type | What it means |
|---|---|
float |
Usually 32-bit precision (suffix f) |
double |
Usually 64-bit precision |
long double |
At least as much precision as double but can provide more (suffix L) |
Top comments (0)