Data Types in C | Set 1

By | February 11, 2023

Data types define the type of data a variable can hold. C language has some predefined set of data types to handle various kinds of data that we can use in our program. These datatypes have different storage capacities.

In C programming, data types are declarations for variables. This determines the type and size of data associated with variables. For example,

int VarName ;

Here, VarName is a variable name and it has type of integer (int).

Category of C Data Types :

Data types in C++ are categorised in three groups: Built-In, Derived, and User Defined Data type.

1. Built-In Data Types :
Built-in data types are the most basic data-types in C. The term built-in means that they are pre-defined in C and can be used directly in a program. char, int, float and double are the most common built-in data types. Apart from these, we also have void and bool data types.

 

Data Type Explanation Example
int To represent integer type values. This has 2 bytes in size. int a = 10;
char To represent character type values. This has 1 bytes in size. char name = ‘A’;
float It is used to store decimal numbers (numbers with floating point value) with single precision. This has 4 bytes in size. float x = 3.14;
double It is used to store decimal numbers (numbers with floating point value) with double precision. This has 8 bytes in size. double num = 10801.98018;
boolean double num = 10098.98899; bool y = true;
void It holds no value. If the function has a void type, it means that the function will not return any value. void foo();

 

Notes –
There are 5 modifiers available in C language. They are: short, long, signed, unsigned, long long.

2. Derived Data Types :
Derived data types are nothing but primary datatypes but a little twisted or grouped together like array, stucture, union and pointer. These are : array, function, and pointers.

  1. Array data type –
    Array can store similar type of data types in contiguous manner. They can be used to store collection of primitive data types such as int, float, double, char, etc of any particular type. Example –

    int a = {10, 20, 30};
  2. Function data type –
    These are used to represent type of function in C. They can be used to store collection of primitive data types such as int, float, double, char, etc of any particular type. Example –

    int foo (int a, int b);
  3. Pointers –
    These are powerful C features which are used to access the memory and deal with their addresses. Example –

    int a;
    int *b = &a;

    Function pointers allow referencing functions with a particular signature.

3. User Defined Data Types :
We can define our own data type using ‘type definition’ in C language. There are three such types : Structure, union, and enum.

  1. Structure Data Type –
    A structure creates a data type that can be used to group items of possibly different types into a single type. “struct” keyword is used to define a structure. Example –

    struct address 
    { 
       char name[50]; 
       char street[100]; 
       char city[50]; 
       char state[20]; 
       int pin; 
    };
  2. Union Data Type –
    Like Structures, union is a user defined data type. In union, all members share the same memory location. “union” keyword is used to define a structure. Example –

    union address 
    { 
       char name[50]; 
       char street[100]; 
       char city[50]; 
       char state[20]; 
       int pin; 
    };
  3. Enumeration Data Type –
    It is used to assign names to the integral constants which makes a program easy to read and maintain. The keyword “enum” is used to declare an enumeration. Example –

    enum week{Mon, Tue, Wed}day;

Read next related article – Data Types in C | Set 2

Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.