C++ Data Types
C++ Data Types
While composing a program in any language, you will need to apply several variables to stock all those information. Variables are emptiness but supplied memory locations to stock the values. It means that when you establish a variable you supply some spaces in the memory.
You maybe like to stock the information of various data types like integer, floating points, double floating points, character, wide character, boolean, etc. Depending on the data types of variables, the operating system assigned memory and make decisions about what can be stocked in the supply memory.
As explained in the Variables chapter, a variable in C++ must be a specified data type:
Basic Data Types
The data type specifies the size and type of information the variable will store:
Data Type | Size | Description |
---|---|---|
int | 4 bytes | Stores whole numbers, without decimals |
float | 4 bytes | Stores fractional numbers, containing one or more decimals. Sufficient for storing 7 decimal digits |
double | 8 bytes | Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits |
boolean | 1 byte | Stores true or false values |
char | 1 byte | Stores a single character/letter/number, or ASCII values |
Note: ASCII: American Standard Code for Information Interchange
Main Categories of C++ Data Types:
Primitive Build-it Types
C++ allows the programmer a lot of classification of built-in as well as user-defined data types. The following table lists seven basic data types in C++:Data Type | Keyword |
---|---|
Boolean | bool |
Character | char |
Floating point | float |
Double floating point | double |
Integer | int |
Valueless | void |
Wide character | wchar_t |
Many basic data types of Cplusplus can be modified by using one or more of these type modifiers:
- signed
- unsigned
- short
- long
The table below shows the types of variables, how much that memory takes to store the value in it, and what is the minimum and maximum value that can be stored in such types of variables.
Date Type | Typical Bit Width | Typical Range |
---|---|---|
char | 1byte | -127 to 127 or 0 to 255 |
unsigned char | 1byte | 0 to 225 |
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,647 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 |
The size of the variables may be different from the above table. It's depending on the device or computer you are using.
The following example will produce the correct size of various data types on the 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;
}
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;
}
Comments
Post a Comment