Signup/Sign In

Datatypes and Modifiers in C++

Let's start with Datatypes. They are used to define type of variables and contents used. Data types define the way you use storage in the programs you write. Data types can be of two types:

  1. Built-in Datatypes
  2. User-defined or Abstract Datatypes

Built-in Data Types

These are the datatypes which are predefined and are wired directly into the compiler. For eg: int, char etc.


User defined or Abstract data types

These are the type, that user creates as a class or a structure. In C++ these are classes where as in C language user-defined datatypes were implemented as structures.


Basic Built in Datatypes in C++

charfor character storage (1 byte)
intfor integral number (2 bytes)
floatsingle precision floating point (4 bytes)
doubledouble precision floating point numbers (8 bytes)

Example:

char a = 'A';          // character type
int a = 1;             // integer type
float a = 3.14159;     // floating point type 
double a = 6e-4;       // double type (e is for exponential)

Other Built in Datatypes in C++

boolBoolean (True or False)
voidWithout any Value
wchar_tWide Character

Enum as Datatype in C++

Enumerated type declares a new type-name along with a sequence of values containing identifiers which has values starting from 0 and incrementing by 1 every time.

For Example:

enum day(mon, tues, wed, thurs, fri) d;

Here an enumeration of days is defined which is represented by the variable d. mon will hold value 0, tue will have 1 and so on. We can also explicitly assign values, like, enum day(mon, tue=7, wed);. Here, mon will be 0, tue will be assigned 7, so wed will get value 8.


Modifiers in C++

In C++, special words(called modifiers) can be used to modify the meaning of the predefined built-in data types and expand them to a much larger set. There are four datatype modifiers in C++, they are:

  1. long
  2. short
  3. signed
  4. unsigned

The above mentioned modifiers can be used along with built in datatypes to make them more precise and even expand their range.

Below mentioned are some important points you must know about the modifiers,

  • long and short modify the maximum and minimum values that a data type will hold.
  • A plain int must have a minimum size of short.
  • Size hierarchy : short int < int < long int
  • Size hierarchy for floating point numbers is : float < double < long double
  • long float is not a legal type and there are no short floating point numbers.
  • Signed types includes both positive and negative numbers and is the default type.
  • Unsigned, numbers are always without any sign, that is always positive.