Data type in C, Built-in and derived data type with examples

Data Type

  • Data Type describes the variable of which type of value can be hold by that particular variable.
  • Suppose we want to save an integer value in a memory and so we need a variable of same integer type which refer the memory location of that integer value.
  • Data type describes the capability of variable that can hold the reference of data/value.
  • If you want to store integer value in memory then the data type of variable should be int where you can assign your integer value.

For example, int, char, float, etc. are the data types.

Example:

int data;
data = 20;

In the above example, we have declared a variable name “data”. Its data type is int, means that we can assign only integer value  to this variable “data” not character value. 

Classification of C data types

TypeData Types
Basic data typesint, char, float, double
Derived data typepointer, array, structure, union
Enumeration data typeenum
Void data typevoid

List of all data types with storage size and value range

TypeStorage sizeValue range
char1 byte-128 to 127 or 0 to 255
unsigned char1 byte0 to 255
signed char1 byte-128 to 127
int2 or 4 bytes-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int2 or 4 bytes0 to 65,535 or 0 to 4,294,967,295
short2 bytes-32,768 to 32,767
unsigned short2 bytes0 to 65,535
long8 bytes-9223372036854775808 to 9223372036854775807
unsigned long8 bytes0 to 18446744073709551615
unsigned long8 bytes0 to 18446744073709551615
unsigned long8 bytes0 to 18446744073709551615
unsigned long8 bytes0 to 18446744073709551615
float4 byte1.2E-38 to 3.4E+38
double8 byte2.3E-308 to 1.7E+308
long double10 byte3.4E-4932 to 1.1E+4932

Explanation of the Data Types in C

Built-in Data Types

1). int data type in C

int is a primitive data type that allows a variable to hold reference of numeric or integer values. On that value we can apply any mathematical operations. Basically Integers are a whole numbers which may be both zero, positive and negative values but no decimal values.
For example, 0, -1, 2 are the integers.

  • “int” is used to declare an integer variable.
  • Storage size of int data type is either 2 or 4 or 8 byte. It depends upon our processor which we use.
  • If we use 16 bit processor then 2 byte (16 bit) memory is allocated to the int data type.
  • If we use 32 bit processor then 4 byte (32 bit) memory is allocated to the int data type.
  • If we use 64 bit processor then 8 byte (64 bit) memory is allocated to the int data type.
  • 2 byte int can store the values between the range from -32,768 to +32,767 (it is for signed int).
  • 4 byte int can store the values between the range from -2,147,483,648 to +2,147,483,647 (it is for signed int).
  • %d is used to display a floating-point value at the terminal.

Some Important points to remember

  • “int” can’t store decimal values .
  • If we try to store decimal values to int variable then decimal values will be truncated and only whole number is stored in the variable.
  • To store decimal value float data type can be used.

For example:

int roll, age;

2). float data type in C

A float type variable is used to store the values containing decimal places. A floating-point constant can be distinguished by the presence of a decimal point in the constants. We can remove the digits before or digits after the decimal point. The values 5., 62.8, and −.0010 are the valid examples of the floating-point constants.

  • “float” is used to declare an float variable.
  • Storage size of float data type is 4. This also varies depend upon the processor in the CPU as “int” data type.
  • To store decimal value “float” data type can be used.
  • Using “float” data type we can assign float variable up-to 6 digits after decimal value.
  • for the example we can store 80.643654 in a variable using float data type.
  • %f is used to display a floating-point value at the terminal.

Some Important points to remember

  • By default C compiler takes floating-point constants as double values.
  • If we want explicitly express a float constant, then append either an F or f to the end of the number like 12.5f.

For example:

float salary = 12.3f;

3). double data type in C

The double is very similar to the float. Double is used when the range provided is not sufficient for a float to store. Variables which is declared of type double can store roughly twice as many significant digits as float variable can store.

  • “double” is used to declare an double variable.
  • Storage size of double data type is 8. This also varies depend upon the processor in the CPU as “float” data type.
  • The range for double datatype is from 1E–37 to 1E+37.
  • To store decimal value double data type can be used.
  • Using double data type we assign value to double variable up-to 10 digits after decimal.
  • for the example we can store 80.6436544323 in a variable using double data type.
  • %f, %e, or %g, is used to display a double value at the terminal.

For Example:

double price;
price = 12.50;

4). char data type in C

Char is a data type that stores a single character or one-character value. A Pair of a single quotation mark is used to represents a character constant.
Some valid example of character are ‘b’, ‘;’, and ‘0’.
In the example first constant represents the letter b, the second is a semicolon, and the third one is an integer, but when we enclose it with a pair of single quotation then it behaves like a character.

  • “char” is used to declare an character variable.
  • Storage size of char data type is 1.
  • The range for double datatype is from -128 to 127 (for signed char).
  • We can’t store more than one character in a variable of char data type.
  • for the example we can store ‘b’, ‘;’, and ‘0’ in a variable using char data type.
  • %c is used to display a char value at the terminal.

For example:

char var = 'a';
The size of the character is 1 byte.

Derived Data Type

In C, there are four derived datatypes Array, structure, pointer, and union.

1) Array

An array is a container that holds similar type of data (values) i.e. every value has similar type of data type. It takes memory in a contiguous fashion.

  • Array can be belong to any of the data types.
  • The size of Array must be a constant value.
  • Array always takes Contiguous (adjacent) memory locations to store the elements in memory.
  • The best practice is that we should initialize an array to zero or null while declaring, if we don’t then assign any values to array.

For Example:

char a[10]; // character array
int b[10]; // integer array.

2). Pointer

Pointers is a variable that stores the address of another variable of the same data type to itself. For dynamic memory allocation i.e., to allocate memory at the run time pointer is used. It can have any of the data types such as int, float, char, double, short, etc.
A pointer variable can store the address of only the same type of datatype. Suppose if we declare a pointer variable of int data type, then this pointer variable can only store the address of int type variable.

Pointer Syntax :

data_type *var_name;
  • Normal variable stores the value but pointer variable stores the address of the variable.
  • C pointer always contains a whole number i.e. address.
  • C pointer always initialized to null, i.e. int *p = null.
  • The value of null pointer is 0.
  • & symbol is used to get the address of the variable.
  • If we assigned NULL to pointer it means that is pointing to nothing.
  • We can subtracted the two pointers to know how many elements are available between them.
  • Addition, multiplication, division are not allowed on the pointer variables.
  • The size of any pointer is 2 byte (for 16 bit compiler).

Example : 

int *a; 
char *a;

Where * is used to denote that “a” is pointer variable which stores the address of variable.

3). Structure

The structure is a collection of variables or items of different types under a single name. We can consider structure as a record. It is very similar to the class of java without methods.
Using Structures, or structs, we can create larger and more complex data structures.

  • To access the structure members in C, It should ensure that the structure variable is declared.
  • We can declare many structure variables for the same structure and memory will be allocated for each variable separately.
  • It is a best practice to initialize a structure to null while declaring if we don’t assign any values to structure members.
  • A structure can hold the group of data having different data types. These Data types can be int, char, float, double and long double, etc.

Why Structure?

For example, suppose if we want to store the information of students: his/her name, roll number, and class. So for that purpose, we need to create a different variable such as name, roll no, and class to store this information separately.

It is good if there is only a single student, but the problem will arise if there are thousands of students and much more. Because if we follow the traditional approach, then we can do it like name1, rollNo1, class1, name2, rollNo2, class2, etc. But it would be very difficult to manage the details of the students.

Think It would be the good approach if we can store all the details of a single student in a single variable. Yes, for that purpose we will choose the structure.
A better approach would be to have a collection of all related information under a single name Student structure and use it for every student.

Syntax of structure:

struct structure_name 
{
    data_type member1;
    data_type member2;
    .
    .
    data_type memeber;
};


Here is an example:

struct Student
{
    char name[50];
    int rollNo;
    int class;
};

4). Union

The union is very similar to the structure in C programming, and it is also a user-defined data type. Like structure, it grouped different data types. In a union, each element is called a member.

  • In C, Structure, and union are the same in concepts, except allocating memory for their members.
  • For the all its member’s Structure allocates storage space separately.
  • But in Union, It allocates one common storage space for all its members
  • In union, we can access only one member at a time. We can’t access the values of all member at the same time in a union.
  • It is only because the Union allocates one common storage space for all its members. Whereas Structure allocates storage space for all its members separately.

Syntax:

union union_name 
{
    data_type member1;
    data_type member2;
    .
    .
    data_type memeber;
};

Example:

union student
{
    char name[50];
    int rollNo;
    int class;
};     

Modifiers in C

What are modifiers?

(It is also a part of data types)
Modifiers derive the amount of memory space which is to be allocated for a variable. Modifiers are prefixed with basic data types to either increase or decrease the amount of storage space allocated to a variable.

a). short and long

Short and long are working as a modifier for the C datatypes. If we need to use a small or large number, then we can use a type specifier short or long.

  • By default, int data type storage is 4 byte for a 32-bit processor. Now by prefixing with the modifier long, we can increase the range which is 8 byte. And also we can decrease the range by using short int which is 2 byte.

Here’s how:

short int a;
int b;
long int c;

Here variables a and b can store integer values. And, c can store a floating-point number.
You can always check the size of a variable using the sizeof() operator.

#include       
int main() {
  short int a;
    int b;
    long int c;
   printf("size of short int = %d bytesn", sizeof(a));
  printf("size of int = %d bytesn", sizeof(b));
  printf("size of long int= %d bytesn", sizeof(c));
  return 0;
}

Output :

size of short int = 2 bytes                                  
size of long int = 8 bytes                                   
size of int= 4 bytes                                 

b). signed and unsigned

Signed and unsigned both are the modifiers. An unsigned variable can hold zero and positive values, and a signed variable can hold negative, zero, and positive values. We can also alter the storage of a data type by using them.

For example
unsigned int a;
int b; //by default it is signed

In the above examples, the variable ‘a’ can hold only zero and positive values because we have used the unsigned modifier before int.
And variable b can store negative, zero and positive values.
Considering the size of int is 4 bytes, variable b can hold values from -231-1 to 231 which is –2,147,483,648 to 2,147,483,647. , whereas variable a can hold values from 0 to 232-1 which is 0 to 4,294,967,295 .

Enumeration data type

Enumeration (or enum) is a user-defined data type that consists of integral constants. In enumeration, we mainly assign names to integral constants, that makes the program easy to read and maintain.

  • enum keyword is used to define an enumeration.
  • Enumeration is a user defined data type, consists of named integer constants as a list.
  • By default it starts with 0 (zero) and incremented by 1 in the list.

For example:
enum temp { const1, const2, …, constN }; Here, name of the enumeration is temp. Void data type A void is an empty data type or incomplete data type, which means “nothing” or “no type.” The void is used with a function which does not return anything. We cannot use void with the variables, and it is only allowed with the functions.

void sum(){ 
sum = 4+5; printf("%d",sum); 
}

Leave a Comment