Data Types in C++

The basic in any programming language is the variables where you store the data. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

You may like to store information of various data types in c++ like character, wide character, integer, floating point, double floating point, boolean etc. 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:

 
TypeKeyword
Booleanbool
Characterchar
Integerint
Floating pointfloat
Double floating pointdouble
Valuelessvoid
Wide characterwchar_t
Several of the basic types can be modified using one or more of these type modifiers:
  • signed
  • unsigned
  • short
  • long

Size of each Data Type:

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 intRange0 to 65,535
signed short intRange-32768 to 32767
long int4bytes-2,147,483,647 to 2,147,483,647
signed long int4bytessame as long int
unsigned long int4bytes0 to 4,294,967,295
float4bytes+/- 3.4e +/- 38 (~7 digits)
double8bytes+/- 1.7e +/- 308 (~15 digits)
long double8bytes+/- 1.7e +/- 308 (~15 digits)
wchar_t2 or 4 bytes1 wide character

NOTE: The sizes of variables might be different from those shown in the above table, depending on the compiler and the computer you are using.

typedef Declarations:

You can create a new name for an existing type using typedef. Following is the simple syntax to define a new type using typedef:
 
typedef type newname;
For example, the following tells the compiler that feet is another name for int:
 
typedef int feet;
Now, the following declaration is perfectly legal and creates an integer variable called distance:
 
feet distance;

Enumerated Types:

An enumerated type declares an optional type name and a set of zero or more identifiers that can be used as values of the type. Each enumerator is a constant whose type is the enumeration.
 
To create an enumeration requires the use of the keyword enum. The general form of an enumeration type is:
 
enum enum-name { list of names } var-list;
Here, the enum-name is the enumeration’s type name. The list of names is comma separated.
 
For example, the following code defines an enumeration of colors called colors and the variable c of type color. Finally, c is assigned the value “blue”.
 
enum color { red, green, blue } c;
c = blue;
By default, the value of the first name is 0, the second name has the value 1, the third has the value 2, and so on. But you can give a name a specific value by adding an initializer. For example, in the following enumeration, green will have the value 5.
 
enum color { red, green=5, blue };
Here, blue will have a value of 6 because each name will be one greater than the one that precedes it.

1 thought on “Data Types in C++”

  1. I would strongly recommend using the new (since C++11) method of defining type aliases:

    using newtype = oldtype;

    instead of typedefs

    typedef oldtype newtype;

    The problem I have with typedefs is that after more than 20 years of C++ programming, I still have to stop and think “which one is the new type”. The new syntax is much clearer in that respect.

    Reply

Leave a Reply to ferruccio Cancel reply