Data Types : C Programming Language

Data Type is the classification of the data that is taken as input, processed and results in an output. It is how we categorize data according to its type. There are three type of Data Types:

  • Primary data types
  • Secondary data types
  • User-defined data types

These data types are further subdivided into several other data types which you can find in the figure below:


Now let’s talk about these data types in detail.

Primary Data Types

Primary data type , also known as ‘primitive data type’ or ‘fundamental data type’, are the built-in data types that are provided by the programming language. It defines the most basic data like int, char, float,etc. Primary data types could be of several types like an int can be unsigned int, short int, unsigned long int,etc. With such a wide range of classification and variety, the programmer has got many different data types to choose from as per the requirement and use it in their code along with the advantage of the secondary data types. Here are the most commonly used primary data types:

  • boolean
  • byte
  • char
  • short
  • int
  • long
  • float
  • double

‘Void’ is another primary data type which means ‘no value’. It is usually used to define the type of return value in a function. A function with void return type does not return any value.

Integer Data Type

Integer data types are used to define the variables taking integer values with or without constant values given to them. The most commonly used keyword or the data type used to define the integer type data is ‘int’. There are other data types like ‘short’ and ‘long’ used to define integer values but they have different ranges (we use them as per the requirement of the program i.e. if we are sure that our requirement is small and it will never go beyond the range of small we shall take small and likewise). In the tutorial constants, we saw that the range of the constants differed for different compilers. Similarly, the size of the  integer data type (in bytes) also differs for different compilers as shown in the table below:

size of int data types

Note that the size of the given data types is in bytes.

The short integer can be used in places where small values and little storage space is required. It can boost up the runtime because it uses less space. It is declared by using the keyword ‘short’ or ‘short int’.

The long integer gives us a long range or a bigger size compared to ‘short’ but it can cause our program to take more time for execution because of the storage size it offers. It is declared by using the keyword ‘long’ or ‘long int’. The range for a long integer is –2147483648 to +2147483647.

This is how we declare the integer variables:

int num,length;
short breadth;
short int height;
long int volume=0;

We have another classification of the integer data type: signed and unsigned int. In case we need to take positive values only, then we can use unsigned int . Its range is 0 to 4294967295. This is because the left most bit is free and does not need to store the sign of the number. So, we get more storage space i.e. double on the positive side. By default, signed type is declared and we do not need to use signed. Signed int works as an int. It’s range is the same as int .

Character Data Type

Character data types are used to define variables taking one character as its value. The keyword used for character data type is ‘char’. Here is how we declare character variables:

char ch, ch1=‘A’, ch2=67;

Here, in ch1 variable, we store ‘A’, i.e., the binary equivalent of the ASCII value of A(=binary of decimal 65) gets stored. And ch2 variable stores the value 67(ASCII for ‘C’). So these are actually two ways of initializing a character value.

Like integers, here we have signed and unsigned character values. The signed char is equivalent to char. For a signed char the range is -128 to +127. Whereas, for an unsigned char the range is from 0 to 255. Here’s how we declare signed(declared as char above) and unsigned char values:

unsigned char ch;

char ch1=128;

Surprised to see why I put the value of ch1 as 128? Here’s another thing about this data type. As mentioned before char has a range of +127 but we have put the value 128 here. What is going to be the output?

What happens here is that once we reach the end of the range, the other side of the range is accessed. It goes back to -128 again. So in this case, when it comes across 128 it goes back to the beginning and accesses the char at the ASCII -128.

Float(and Double) Data Type

Float and double data types are used to define variables that take up a decimal value or an exponential value. The keyword used for float and double data type are ‘float’ and ‘double’ respectively.

  • Float has a range of –3.4e38 to +3.4e38 and its size is 4 bytes.
  • Double has a range of -1.7e308 to +1.7e308 and its size is 8 bytes.
  • Another data type that is offered by programming languages is ‘long double’ which has a range of –1.7e4932 to +1.7e4932 and its size is 10 bytes. Here is how we declare float and double variables:

float length, area=0.0;

double radius, area=0.0;

Secondary Data Types

Secondary data types are basically derived from the primary data types. Let’s have a look at a few secondary data types:

  • Array: An array is a collection of data of the same data type. These are declared under the same variable and are accessed using it. If we declare an integer array, all the values in the array have to be an integer. Similarly, for a character array all the elements of the array are characters and same goes for double and every other data type. An array is declared as follows:
    int a[50]; //Declaration
    This array has an integer data type and can store 50 integer elements.
  • Pointers: A pointer contains the address of a variable in the program .
    We declare the pointer as:
    int *ip; //Declaration
    A pointer declared as integer type stores the address of the integer type variable. Similarly, a pointer declared as char type stores the address of character type variable and so on.

We will discuss about each one of them in detail in future posts.

User-defined data types

User-defined data type defines the data in the way that the programmer chooses. Let’s have a look at these commonly used user-defined data types:

  • Structures: It is a collection of variables of different data types represented by the same name. Unlike array where we had to store all the data of the same type in the variable, here one can store data of different data types under the same variable name. It is mostly used to form records where different specifications needs to be stored under the same name. The struct keyword is used to define a structure.
  • Union: Another data type which is very similar to structures. It allows the programmer to store data of different data type in the same memory location. A union can have multiple members but it can store only one member at a particular time. The keyword union is used to define a Union.
  • Enum: Enum or Enumeration is used to declare the variables and consists of integral constants. The keyword enum is used to define the enumeration data type.
    Eg: enum identifier{element1, element2,……., elementn};  It assigns the value from 0 to n to the elements present inside the identifier sequentially.

Knowledge is most useful when liberated and shared. Share this to motivate us to keep writing such online tutorials for free and do comment if anything is missing or wrong or you need any kind of help.

Do not forget to share and Subscribe.

Keep Learning… Happy Learning.. :)