Data Types

Data Type:

  • Defines the type of data that the associated variable can store.
  • Defines the type of operations that can be performed on the associated variable (or on actual data).
  • Defines the size of memory to be allocated by the compiler.

Properties of Data Types:

  • Size: Memory – i.e., no. of bytes required by the data type.
  • Range: no. of bits (in the allocated memory) that can be set to ‘1’ (TRUE).

C Data Types:

  1. void – not used to declare variable, it used for function return types and type casting.
  2. char
  3. int
  4. float
  5. double

Example Program:

The below program covers all the C subset data types and outputs the size of each data type on my machine.

#include 
void main()
{
    char c;
    int i;
    float f;
    double d;
    printf("size of char    : %lu n", sizeof(c));
    printf("size of int     : %lu n", sizeof(i));
    printf("size of float   : %lu n", sizeof(f));
    printf("size of double  : %lu n", sizeof(d));
}

Output:

size of char : 1
size of int : 4
size of float : 4
size of double : 8

C++ Data Types:

In addition to the data types available in C subset, below are two more data types added by C++.

  1. bool
  2. wchar_t

Example Program:

The below program covers all the C++ subset data types and outputs the size of each data type on my machine.

#include 
using namespace std;
int main()
{
    bool b;
    wchar_t wc;
    cout << "size of bool   : " << sizeof(b) << endl;
    cout << "size of wchar   : " << sizeof(wc) << endl;
    return 0;
}

Output:

size of bool   : 1
size of wchar   : 4