These tutorials are for both C and C++. In these examples are given for both languages. If you are learning C you will consider examples of C language or else C++.

Before we moved to variable let discuss little bit about data types. Data types in C/C++ is general system to declare variables and functions (return type) of different types. Data type determines how many bytes a variable can store and in what manner it’s going to store it. Data types define bit pattern for a variable to store.

Data types are classified as follows:

1.      Basic Types

Also known as built-in data types i.e. int (to store integer), float (to store floating values) and char (to store character)

2.      Enumerated Types

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.

3.      The type void

This is special data type that defines no value is store or no value is returned by the function.

4.      The derived types

Derived types mean composite data. Which means these data types can store different values that can be combination of any of the data types mentioned above. Derived types are of 2 types:

  1. Directly Derived Types: Include Arrays, Functions, Constants, and Pointers.
  2. Composed Derived Types: Includes Structures, Classes and Unions

In this tutorial we’ll only discuss Basic data types whereas other types will be discussed in later chapters.

Basic Data Types

Basic or built in data types in C/C++ include 4 basic arithmetic type specifier int, float, char and double other optional types are signed, unsigned, short and long. These 4 optional are not data types they are just used with basic data types to modify their actual behavior i.e. short can be used with int to store integer value but it’ll consume less memory or you can say it’ll store less value then actual int data type.

Below is the comparison table for all data types

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,648 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)

Size or range of a data type may vary from compiler to complier above table is of 32-bit compiler. 64-bit compiler can store double of above values for each type except for some types like char.

You can use sizeof(type) operator to know exact size of which a single data type can store.

C Example:


#include <stdio.h>
#include <conio.h>
#include <limits.h>

void main()
{
   printf("Size of char : %c \n", sizeof(char));
   printf("Size of int : %d \n", sizeof(int));
   printf("Size of short int : %hi \n", sizeof(short int));
   printf("Size of long int : %li \n", sizeof(long int));
   printf("Size of float : %.2f \n", sizeof(float));
   printf("Size of double : %.2f \n", sizeof(double));
   
   getch();
}

C++ Example:


#include <iostream>
using namespace std;

int main()
{
   cout << "Size of char : " << sizeof(char) << endl;
   cout << "Size of int : " << sizeof(int) << endl;
   cout << "Size of short int : " << sizeof(short int) << endl;
   cout << "Size of long int : " << sizeof(long int) << endl;
   cout << "Size of float : " << sizeof(float) << endl;
   cout << "Size of double : " << sizeof(double) << endl;

   return 0;
}

he void Data Types

void is used when no value is available. It is special kind of data type that can used in 3 situations

  1. A function with no return value has return type void which indicates that function is not return any value.
  2. Function argument as void means function is not accepting or function is not getting any argument or function doesn’t have any parameters.
  3. A pointer of void data type represents variables address but it doesn’t represent its data type.

We’ll discuss these points later in each section.