C Data Types

In C programming language, a data type determines the type of data that can be stored in variable(Variable is a name given to the memory location). Data type is mainly used to save memory space.

C Data Types
C Data Types

In C programming language, a data type determines the type of data that can be stored in variable(Variable is a name given to the memory location). Data type is mainly used to save memory space.

There are different types of data types in C programming language. They are,

Types Data types
Basic Data Type int, char, float, double
Enumerated Data Type enum
Void Data Type void
Derived Data Type array, pointer, structure, union, function

Basic Data Type

Basic data type is also called as primitive data type. Basic data type are predefined data types in C programming language.

Integer Data type

Integer data type is used to store the whole numbers. Integer data type has three classes of integer storage, they are short int, int and long int and all the three classes can be used as signed integers or as unsigned integers.

Type Size(in byte) Range Control string
signed short int 1 -128 to 127 %h
unsigned short int 1 0 to 255 %uh
int or signed int 2 -32768 to 32767 %d or %i
unsigned int 2 0 to 65535 %u
signed long int 4 -2147483648 to 2147483647 %ld
unsigned long int 4 0 to 4294967295 %lu

Character data type

Character data type are internally stored as integers and it is used to store single character. Each character has its equivalent ASCII value. Eg: Character ‘A’ has ASCII value 65.

Type Size(in byte) Range Control string
char or signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c

Floating point data type

Float data type is used to store real numbers that’s decimal point numbers.

Type Size(in byte) Range Control string
float 11 3.4E – 38 to 3.4E + 38 %f
double 8 1.7E – 308 to 1.7E + 308 %lf
long double 10 3.4E – 4932 to 1.1E + 4932 %Lf

Note:

  • float data type will take up to 6 digits after the decimal point. Eg: 12.6234
  • double data type will take up to 12 digits after the decimal point. Eg: 7.983457989
  • long double data type allow us to take more than 12 digits after the decimal point. Eg: 3.98345672343743892

Example

#include
#include
int main()
{
    int a;
    float b;
    char c;
    double d;
    printf("The memory allocated for integer data type is %d \n", sizeof(a));
    printf("The memory allocated for float data type is %d \n", sizeof(b));
    printf("The memory allocated for character data type is %d \n", sizeof(c));
    printf("The memory allocated for double data type is %d \n", sizeof(d));
    return 0;
}

Output

The memory allocated for integer data type is 4
The memory allocated for float data type is 4
The memory allocated for character data type is 1
The memory allocated for double data type is 8

Enumerated Data Type

Enumerated data type is also called Enum data type which consist of named integer constant as a list. When we declare enum data type it starts with 0 by default and the the values will be incremented by 1 in a sequential.

Syntax:

enum identifier[optional {enumeration_list}];

Example

enum month{Jan, Feb, Mar}

The variables Jan, Feb, and Mar are assigned to 0, 1, and 2 by default.

enum month{Jan = 10, Feb, Mar}

The variables Feb and Mar are assigned as 11 and 12 by default, because enum data type increment by one.

Example program

#include
int main()
{
        enum month{Jan = 10, Feb, Mar};
        if(Mar == 12)
            printf("The default value assigned for Mar is 12");
        return 0;
}

Output

The default value assigned for Mar is 12

Void data type

If we define a function which is not returning any value, we can mark the return type as void. If we don’t want to pass any parameter we can leave it as empty.

Example

#include
void main()
{
        printf("This is an example for void data type");
        return ;
}

Output

This is an example for void data type