Data types in c++

Variables in programming language are same as variables in mathematics, in a sense that their value also varies they are also defined in a similar fashion E.g. x = 5; here x is a variable which has a value 5 stored in it. Variables are used to reserve memory locations to store values.
There are types of variables so information of different types can be stored on it. For example, int for storing integer and bool for storing Boolean value.
Primitive Built-in Types
C++ offer the programmer a rich assortment of built-in as well as user defined data types. Following table lists down seven basic C++ data types:
Type
Keyword
Boolean
bool
Character
char
Integer
int
Floating point
float
Double floating point
double
The following 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
-128 to 127 or 0 to 255
unsigned char
1byte
0 to 255
signed char
1byte
-128 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
8bytes
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
signed long int
8bytes
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long int
8bytes
0 to 18,446,744,073,709,551,615
float
4bytes
+/- 3.4e +/- 38 (~7 digits)
double
8bytes
+/- 1.7e +/- 308 (~15 digits)
long double
8bytes
+/- 1.7e +/- 308 (~15 digits)
The sizes of variables might be different from those shown in the above table, depending on the compiler and the computer you are using. You can use sizeof () method to find the size. For example
#include
using namespace std;

int main () {
   cout << "Size of char : " << sizeof(char);
   return 0;
}
After code is compiled following output will be produced :
Size of char : 1


Comments

Popular posts from this blog

Comments in C++

Types of variables in C++