In any programming language while writing the code we need to manipulate the data provided to us. The data value can vary from its initial value throughout the code. The location to store data in some memory locations needs to be referenced in the program in a convenient way. The name given to these memory locations is known as Variables.
When we create a variable we reserve space in the temporary memory. Now, how much data should a variable reserve? It depends on what kind of data you want to store in it? Suppose you probably need more memory space to occupy your name than your phone number as Alphabets first gets converted into ASCII value and then is stored. So how would our program know how much memory to be reserved for our program?
This is taken care by the variables data types.
Data types of variable tell about the type of data it will store in this variable thus telling the system to reserve so much space for the variable. Data like character, string, integer, floating point, very long integers etc.
Some Primitive Data types in CPP
Keyword | Type | Functionality |
bool | Boolean | Can have true or false value |
char | Character | Can store characters |
int | Integer | Can store integers |
float | Floating point | Can store integers with decimal |
double | Double floating point | Supports bigger value than float |
void | Void(without any value) | Just a valueless variable |
These are some of the widely used data types in every programming language though these terms can be modified according to your uses like by adding modifiers before them
Modifier | Functionality |
Signed | This is by default set before any data type |
Unsigned | This doubles the range of data type by removing negative counterpart |
Short | This reduces the range of data type by reducing bytes of memory allocated |
long | Increases the range of data type by increasing bytes of memory allocated |
Let’s understand it in a much better way with the help of the following table
Type | Typical Bit Width | Typical Range |
char | 1byte | -127 to 127 or 0 to 255 |
unsigned char | 1byte | 0 to 255 |
signed char | 1byte | -127 to 127 |
int | 4bytes | -2147483648 to 2147483647 |
unsigned int | 4bytes | 0 to 4294967295 |
signed int | 4bytes | -2147483648 to 2147483647 |
short int | 2bytes | -32768 to 32767 |
unsigned short int | 2bytes | 0 to 65,535 |
signed short int | 2bytes | -32768 to 32767 |
long int | 4bytes | -2,147,483,648 to 2,147,483,647 |
signed long int | 4bytes | same as long int |
unsigned long int | 4bytes | 0 to 4,294,967,295 |
float | 4bytes | +/- 3.4e +/- 38 (~7 digits) |
double | 8bytes | +/- 1.7e +/- 308 (~15 digits) |
long double | 8bytes | +/- 1.7e +/- 308 (~15 digits) |
The size of the variable might be different on different machines depending upon the compiler and the computer you are using. An important function sizeof (); will let you know the exact size of the variable on your machine. Following lines will produce the correct size of the different data types on your machine.
cout<<"size of int"<<sizeof(int)<<endl;
typedef Declarations
When you are writing some predefined word repeatedly whose syntax is quite big to write or if you are shifting from other language and you want to change some syntax according to your need you can use typedef keyword, it basically tells about the usage of a keyword beside existing declarations.
For example
typedef float decimal;
After writing this now you could easily use decimal instead of float and it will work same. Typedef tells the compiler to replace this word "decimal" with the word "float" everywhere.
Enumerated Types:
The CPP enumeration syntax exists to support the use of human-readable character names to support a specific list of available values for the specified variable. This is enabled in C and CPP as a set of named integer constants. This can be expressed as #define preprocessor directives or as a C or C++ enumerated type "enum". Both C and C++ use the same enumeration syntax. An example of a C++ enumeration class which extends the capability of the C enumeration is as follow:
The list of name is comma separated list take an example
By default, the value of the first name is 0, the second name has the value 1, and the third has the value 2, and so on. Here for reference, Delhi will have 0, Mumbai will have value 1, Jaipur will have value 2, Bangalore will have value 3.