C++Builder Quickly Learn About Data Types And Size Of Variables In The C++ Programming Language

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Quickly Learn About Data Types And Size Of Variables In The C++ Programming Language
By Yilmaz Yoru March 18, 2021

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

boolBoolean ( True or False)
charCharacter in ASCII Mode ( 0 to 255)
intInteger Number
floatFloating Point Number
doubleDouble Floating Point Number
voidValueless
wchar_tWide character

We can use int, float, double with prefixes signed, unsigned, short, long, long long.
Код:
#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.

TypeTypical Bit WidthTypical Range
char1byte-127 to 127 or 0 to 255
unsigned char1byte0 to 255
signed char1byte-127 to 127
int4bytes-2147483648 to 2147483647
unsigned int4bytes0 to 4294967295
signed int4bytes-2147483648 to 2147483647
short int2bytes-32768 to 32767
unsigned short int2bytes0 to 65,535
signed short int2bytes-32768 to 32767
long int8bytes-2,147,483,648 to 2,147,483,647
signed long int8bytessame as long int
unsigned long int8bytes0 to 4,294,967,295
long long int8bytes-(2^63) to (2^63)-1
unsigned long long int8bytes0 to 18,446,744,073,709,551,615
float4bytes
double8bytes
long double12bytes
wchar_t2 or 4 bytes1 wide character