In C++ Programming Language, the information is stored with the various data types like character, wide character, integer, floating point, double floating point, boolean etc.
When coding in any programming language, we need to use various variables to store various information. Each variable reserves a location in memory, reading a variable from this memory location or writing a variable to this memory location takes time and they took space in the memory.
In Today’s technology when we have Petabytes and over 5+ Ghz CPUs and RAMs in frequencies, these may seem like no meaning but a good program is the optimized program by developer and everything makes sense on runtime. This reading and writing operations may be took 1/millions of seconds or milliseconds, and users has RAM memory in GBs. But for example when you use same function million of times with same variables that makes difference. Probably your function or algorithm will work slow, or some variables may exceed their limits or it will cost a lot of memory. These small details may make big changes in high quality applications. i.e. Data Transferring, Data Storing, Data Mining, Image Processing, Face Detection, AI Technologies, Video Streaming and Coding/Decoding Operations etc.
So, a good developer should know size of data types and their limits Because based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
Primitive Built-in Types
These are the basic C++ data types
bool | Boolean ( True or False) |
char | Character in ASCII Mode ( 0 to 255) |
int | Integer Number |
float | Floating Point Number |
double | Double Floating Point Number |
void | Valueless |
wchar_t | Wide character |
We can use int, float, double with prefixes signed, unsigned, short, long, long long.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> using namespace std; int main(int argc, char** argv) { int a; unsigned int aa; short int b; unsigned short int ub; long int c; unsigned long int uc; long long int d; unsigned long long int ud; return 0; } |
Size of Variable Types
Here is the full table shows the variable type, how much memory it takes to store the value in memory, and what is maximum and minimum value which can be stored in such type of variables.
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 |
long long int | 8bytes | -(2^63) to (2^63)-1 |
unsigned long long int | 8bytes | 0 to 18,446,744,073,709,551,615 |
float | 4bytes | |
double | 8bytes | |
long double | 12bytes | |
wchar_t | 2 or 4 bytes | 1 wide character |
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition