When programming in a programming language, various variables are needed to store various information. What a variable retains is the memory location of the value it stores. This means that when you create a variable, some space is reserved in memory.
You may need to store information of various data types (such as character type, wide character type, integer type, floating point type, double floating point type, Boolean type, etc.). The operating system will depend on the data type of the variable. , to allocate memory and decide what to store in reserved memory.
C++ provides programmers with a rich variety of built-in data types and user-defined data types. The following table lists the seven basic C++ data types:
Type | Keyword |
---|
Boolean Type | bool |
Character type | char |
Integer type | int |
Float type | float |
Double floating point type | double |
No type | void |
Wide character type | wchar_t |
Some basic types can be modified using one or more type modifiers:
signed
unsigned
short
#long
The following table shows the memory required by various variable types to store values in memory. , as well as the maximum and minimum values that variables of this type can store.
Type | Bit | Range |
---|
char | 1 bytes | -128 to 127 or 0 to 255 |
unsigned char | 1 bytes | 0 to 255 |
signed char | 1 byte | -128 to 127 |
int | 4 bytes | -2147483648 to 2147483647 |
##unsigned int | 4 bytes | 0 to 4294967295 |
signed int | 4 bytes | -2147483648 to 2147483647 |
short int | 2 bytes | -32768 to 32767 |
unsigned short int | Range | 0 to 65,535 |
signed short int | Range | -32768 to 32767 |
long int | 4 bytes | -2,147,483,647 to 2,147,483,647 |
signed long int | 4 bytes | Same as long int |
unsigned long int | 4 bytes | 0 to 4,294,967,295 |
float | 4 bytes | +/- 3.4e +/- 38 (~7 digits) |
double | 8 words section | +/- 1.7e +/- 308 (~15 digits) |
long double | 8 bytes | +/- 1.7e +/- 308 (~15 digits) |
wchar_t | 2 or 4 bytes | 1 Wide characters |
As can be seen from the above table, the size of the variable will vary depending on the compiler and the computer used.
The following example will output the size of various data types on your computer.
#include <iostream>
using namespace std;
int main()
{
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
return 0;
}
This example uses endl, which will insert a newline character after each line. The << operator is used to pass multiple values to the screen. We also use the sizeof() function to get the size of various data types.
When the above code is compiled and executed, it produces the following results, which will vary depending on the computer used:
Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4
You can use typedef to give an existing type a new name. The following is the syntax for using typedef to define a new type:
typedef type newname;
For example, the following statement will tell the compiler that feet is another name for int:
typedef int feet;
Now, the following declaration is completely Legal, it creates an integer variable distance:
feet distance;
The enumeration type declares an optional type name and a set of identifiers used as the type value. Zero or more identifiers can be used as values of this type. Each enumerator is a constant of an enumeration type.
To create an enumeration, you need to use the keyword enum. The general form of an enumeration type is:
enum enum-name { list of names } var-list;
Here, enum-name is the name of the enumeration type. The list of names { list of names } is comma separated.
For example, the following code defines a color enumeration, and the type of variable c is color. Finally, c is assigned the value "blue".
enum color { red, green, blue } c;
c = blue;
By default, the first name has a value of 0, the second name has a value of 1, the third name has a value of 2, and so on. However, you can also assign a special value to the name by adding an initial value. For example, in the following enumeration, green has a value of 5.
enum color { red, green=5, blue };
Here, blue has a value of 6 because by default, each name is 1 greater than the one before it.