Data Types in C explanation with simple examples

As C language supports various types of data, each of which may be represented differently within a computer’s memory. Data type is used to determine what type of value a variable or a constant can contain throughout the program. In C language, different variables contain different data types. A program generally contain different types of data types to process that stored data. In C language, it is compulsory to declare variables with their data type before using them in any statement otherwise you will not get the expected output and you will see error message prompted after compilation of the program.

Mainly data types are categorized into 3 categories:-

1. Fundamental Data Types
2. Derived Data Types
3. User Defined Data Types

1. Fundamental Data Types

Fundamental data types are also known as primitive or primary data types. This category contains  basic types a variable can have. Let us discuss each of them.

  • Int (Integer)

Integer data type is used to store numeric values without any decimal point. However there is no restriction, it can take both positive and negative value. E.g. 15,225,-567,-876,98 etc.

Syntax:

int variable name;

Suppose we want to use student’s roll muber and his marks in any program. We will declare it as-

E.g. int roll; int marks;

You can also declare it as int roll,marks;

wing table gives you detail about standard integer types with its storage sizes and value ranges:

Type

        Storage size

Value range

int       2 or 4 bytes     -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int       2 or 4 bytes                     0 to 65,535 or 0 to 4,294,967,295
short          2 bytes                              -32,768 to 32,767
unsigned short         2 bytes                                 0 to 65,535
long         4 bytes                -2,147,483,648 to 2,147,483,647
unsigned long          4 bytes                         0 to 4,294,967,295
  •  Float

Float data type is used to store numeric values with decimal point. In other words, float data type is used to store real values, e.g. 2.12,6.54,-123.6 etc. A variable declared as float must contain decimal values.

Syntax:

Float variable name;

and their precision:

   Type

 Storage size

        Value range

                 Precision

    float      4 byte         1.2E-38 to 3.4E+38                   6 decimal places
   double     8 byte     2.3E-308 to 1.7E+308                   15 decimal places
  long double    10 byte    3.4E-4932 to 1.1E+4932                   19 decimal places
  • Char (Character)

Char (Character) data type is used to store single character, within single quotes e.g. ‘e’, ‘g’,’h’ etc. A variable declared as ‘char’ can store only single character

Syntax:

Char variable name;

Type

        Storage size

                           Value range

char            1 byte                              -128 to 127 or 0 to 255
unsigned char            1 byte                                          0 to 255
signed char            1 byte                                      -128 to 127
  • Void

Void data type is used to represent an empty value. It has following jobs:

  1. It is used as a return type if a function does not return any value.
  2. To indicate empty argument list of a function.
  3. To declare generic pointers.

2. Derived Data Types

Data types that are derived from fundamental data types are called derived data types. Derived data types don’t create a new data type; they only add some functionality to the basic data types. Derived data type are – Array, Pointer, Pointers.

  • Array

An array is a collection of variables of same type i.e. collection of homogeneous data referred by a common name. In memory, array elements are stored in a continuous location. In C Language, arrays starts at position 0. The elements of the array occupy adjacent locations in memory. C Language treats the name of the array as if it were a pointer to the first element. Any item in the array can be accessed through its index, and it can be accesed any where from with in the program

Syntax:

Data type array_name[max];

E.g. int number[10];

array

  • Pointer

A pointer is a variable whose value is the address of another variable ie. direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address. The general form of a pointer variable declaration is:

Data type *var-name;

Where Data type is the type whose address we want to store in the pointer variable.
* is a pointer variable.
‘var name’ is the name where the variable is to be stored.

E.g. if we want to store the address of an ‘int’ data type variable into a pointer variable, it is done in the following manner:

int a,*b;
b=&a;

In this variable ‘b’ stores the address of variable ‘a’.

  • Functions:

A function is a self contained block of statements that perform some specific task. A function groups a number of program statements into a single unit and give it a name.

3. User Defined Data Type

User defined data type is used to create new data types. The new data types formed are fundamental data types. Different user defined data types are: struct, union, enum.

  • Struct

A struct is a user defined data type that stores heterogeneous values or multiple values of same or different data types under a single name. In memory, the entire structure variable is stored in sequence.

Syntax:

Struct < structure name>
{
Var1;
Var2;
—–
—–
};

Structure name is the name of structure that we have to give e.g. if we want to store details of a student as- name, roll, marks then in structure it will be declared in the following manner:

Struct student
{
Char name [20];
Int roll,
Float marks;
};

Now, its variable is declared as: struct

E.g. Struct student s1;

And its variable is accessed using dot (.) operator.

S1.roll, s1.name, s1.marks

If we talk about its total memory consumed, it will be 20 bytes for char, 2 bytes for roll and 4 bytes for marks, so a total of 26 bytes.

  • Union

A union is a user defined data type that stores multiple values of same or different data types under a single name. In memory, union variables are stored in a common memory location.

Syntax:

Union < tag name>
{
Var1;
Var2;
—–
—-
};

Tag name is the name of union, e.g. if we want to store details of a student as- name, roll, marks then it will be done in the following manner
:
Union student
{
Char name [20];
Int roll,
Float marks;
};
Now, the variable is declared as follows:

struct

e.g. Union student s1;

And its variable is accessed by using dot (.) operator.

S1.roll, s1.name, s1.marks

The only difference between Union and Struct is allocation of memory. ‘Union’ is assigned a memory size equal to the biggest data type used in union declaration( i.e 20 bytes) whereas ‘struct’ will occupy the size equal to the sum of all variables sizes (i.e 26 bytes). 

  • Enum:

An enumeration is a data type similar to a struct or a union. Its members are constants that are written as variables, though they have signed integer values. These constant represent values that can be assigned to corresponding enumeration variables.

Syntax:

Enum {value1, value2, _ _ _ _ _ _, value n};

Enum is the required keyword;
Tag is the name that identifies enumerations having this composition;
Value1, value2, – – – – – – , value n represents the individual identifiers that may be assigned to a variable of this type.

E.g. : Enum colors {black, blue, cyan, green, red, yellow};
Color foreground, background;

First line defines enum and second one defines enum variables. Thus, variables foreground and background can be assigned any one of constant black, blue, – – – – ,yellow.

In declaration black is assigned 0 value, and blue 1, – – – -, and yellow 5. We can also assign the value accordingly as we want to assign, like:

Enum colors {black, blue, cyan=4, green, red, yellow}
Here black=0, blue=1, cyan=4, green=5, red=6, yellow=7