In this tutorial we will understand the concept of C++ Variables and Datatypes.
- A variable provides us with a named storage that our programs can manipulate.
- Each variable in C++ has a specific type, which determines the size and layout of the variables memory; the range of values that can be stored within that memory and the set of operations that can be applied on that variable.
- Each variable needs an identifier(name of the variable that we assign) that distinguishes itself from others.
- The name of a variable can be composed of letter, digits, and underscore(_) characters. It must begin with either a word or underscore character.
- Upper and lower case letters are distinct because C++ is case sensitive.
- E.g. int x = 5; char letter = ‘a’;
Identifiers (name that we assign to a variable or any entity in general)
- A valid identifier, is a sequence of one or more letters, digits or underscore(_) characters. Neither spaces nor puncutation marks or symbols can be a part of identifiers.
- Only letters, digits and underscores are allowed.
- Another rule is that while creating these identifiers, use of C++ inbuilt keywords is not allowed. Some keywords (for e.g. for, while, int) are reserved keywords which cannot be used as identifiers.
- The standard reserved keywords are as follows –
- and, double, not_eq, throw, and_eq, dynamic_cast, operator, true, asm, else, or, try, auto, enum, or_eq, typedef, bitand, explicit, private, typeid, bitor, extern, protected, typename, bool, false, public, union, break, oat, register, unsigned, case, fro, reinterpret-cast, using, catch, friend, return, virtual, char, goto, short, void, class, if, signed, volatile, compl, inline, sizeof, wchar_t, const, int, static, while, const-cast, long, static_cast, xor, continue, mutable, struct, xor_eq, default, namespace, switch, delete, new, template, do, not, this
Data Types in C++
- All variables use data-type during declaration to restrict the type of data to be stored.
- Therefore, we can say that data types are used to tell the variables the type of data it can store.
- Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data-type with which it is declared.
- Every data type requires different amount of memory.
In C++ Programming Data types can be categorized broadly in 3 types –
- Primitive/Buit-in Data Types.
- Derived Datatypes.
- User-Defined Data types.
1. Primitive / Built-in Data Types
These contain the basic inbuilt data types provided by C++ itself and are futher classified as follows-
- integral type
- floating type
2. Derived Data Types
These contain datatypes which are built using the basic/primitive datatypes and are provided by C++ itself however they are not considered as the basic types. For example –
- References etc
3. User – Defined Data Types
These datatypes are custom datatypes that we create according to our requirements. Custom datatypes can be combinations of multiple basic datatypes. For example –
- Enumerations etc
C++ Data Types Diagram
Data Type, Size and Range of Primitive Datatypes –
*the value of size and range depends on the system the program is compiled on(they depend on system architecture)
|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||Range||0 to 65,535|
|signed short int||Range||-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)|
|wchar_t||2 or 4 bytes||1 wide character|
// C++ program to sizes of data types
using namespace std;
cout << "Size of char : " << sizeof(char) << " byte" << endl;
cout << "Size of int : " << sizeof(int) << " bytes" << endl;
cout << "Size of short int : " << sizeof(short int) << " bytes" << endl;
cout << "Size of long int : " << sizeof(long int) << " bytes" << endl;
cout << "Size of signed long int : " << sizeof(signed long int) << " bytes" << endl;
cout << "Size of unsigned long int : " << sizeof(unsigned int) << " bytes" << endl;
cout << "Size of float : " << sizeof(float) << " bytes" <<endl;
cout << "Size of double : " << sizeof(double) << " bytes" << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << " bytes" <<endl;