Data types in C Language

In this blog post, you will learn about data types such as char, int, float, double, ..etc (inbuilt types) and structure, union, pointer, array, ..etc (derived types) with the help of programming examples.

So Let’s get started from the introduction of the data types which means let’s understand what data types in C are?

Basically, C data types define the semantics and storage properties of a data element.  For example, if you will create a char variable then it will reserve the 1 byte of the memory but for the short variable 2 bytes of memory. Here char and short are data types that help the compiler to decide the size of storage memory. Data types also determine the types of operations which we can apply to data elements.

Classification of the data types in C:

The C-type system consists of the following types:

C Basic types:

In C programming, basic types are char, signed and unsigned integer types, and floating types. Basic types are complete object types. The size of the basic types can get by the sizeof operator  (in bytes). But their size depends on the platform for example integer size could be 2 bytes, 4bytes; C standard only specifies that int must be 16 bits wide and the size of short no longer than int.

It is the reason the C standard also introduces fixed-width integers like uint8_t, uint16_t uint32_t …etc (C99). These are defined in <stdint.h> header file. You have to include this file before using the fixed-width integers.

 

1. char types in C:

In the C programming language, the size of char is 1 byte. And a char-type object can store any member of the basic execution character set (alpha-numeric, special characters, ..etc).

Consider the below example where the character variable ch is storing an alphabetic character.

#include <stdio.h>

int  main()
{
    //ch char object storing
    // 'P' which is alphabatic char 
    char ch = 'P';

    printf("%c",ch);

    return 0;
}

Output: P

 

One point needs to remember that if a char object-store any member of the basic execution character set, its value is guaranteed to be nonnegative. But if it stores any other character, the resulting value is implementation-defined.

Note: The three types char, signed char, and unsigned char are collectively called the character types.

 

2. int types in C:

In C programming,  the standard integer types can divide into two categories standard signed integer types and standard unsigned integer types. Further standard signed integer types can be divided into five categories, designated signed char, short int, int, long int, and long long int. For each of the signed integer types, there is a corresponding unsigned integer type that uses the same amount of storage (including sign information) and has the same alignment requirements.

The range of nonnegative values of a signed integer type is a subrange of the corresponding unsigned integer type, and the representation of the same value in each type is the same. For example,

unsigned short int: 0 to 65535

signed short int : 0 to 32767 (non-negative value)

You can see a non-negative value of signed short comes between the value unsigned short.

 

Note: A computation involving unsigned operands can never produce an overflow.

 

3. floating types in C:

Right now, there are three standard floating types in C: designated float, double, and long double. The precision of float is less than compared to the other floating types. Also, the precision of the long double is higher than other floating types but in the future standardization may include additional floating types that have greater range, precision, or both than the long double.

 

 

C Program to Find the Size of int, float, double, and char:

In the below C code, I have used the sizeof operator to find the size of the int, char, float, and double variables.

#include<stdio.h>

int main()
{
    int a;
    float b;
    double c;
    char d;

    //size in bytes
    printf("Size of int: %zu bytes\n", sizeof(a));
    printf("Size of float: %zu bytes\n", sizeof(b));
    printf("Size of double: %zu bytes\n", sizeof(c));
    printf("Size of char: %zu byte\n", sizeof(d));

    return 0;
}

Output:

C Program to Find the Size of int float double and char

 

The output of the above could be different on a different platform. Why I am saying that is because the C standard only specifies the below operation.

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

 

In the below table, I have listed some data types with their ranges and format specifier as per the 32-bit GCC compiler.

Data Type             Memory (bytes)          Range                      Format Specifier
short int                   2          -32,768 to 32,767                       %hd
unsigned short int          2           0 to 65,535                            %hu
unsigned int                4           0 to 4,294,967,295                     %u
int                         4          -2,147,483,648 to 2,147,483,647         %d
long int                    4          -2,147,483,648 to 2,147,483,647         %ld
unsigned long int           4           0 to 4,294,967,295                     %lu
long long int               8          -(2^63) to (2^63)-1                     %lld
unsigned long long int      8           0 to 18,446,744,073,709,551,615        %llu
signed char                 1          -128 to 127                             %c 
unsigned char               1           0 to 255                               %c
float                       4               -                                  %f
double                      8               -                                  %lf
long double                 12              -                                  %Lf

 

bool types in C (Since C99):

You need to include header file​ <stdbool.h> before using the bool in C. A boolean object is large enough to store the value true and false. Let’s see an example of how to create bool variables and use them in boolean operation.

#include <stdio.h>
#include <stdbool.h>

int main()
{
    //bool type variables
    bool a=true;
    bool b=false;

    //doing boolean operations
    printf("a&&b = %d\n\n", a&&b);

    printf("a||b = %d\n\n", a||b);

    printf("!b = %d\n", !b);

    return 0;
}

Output:

a&&b = 0

a||b = 1

!b = 1

 

Enumerated types:

An enumeration type contains a set of named integer constant values. A variable of the enumeration type stores one of the values of the enumeration set defined by that type. Consider the below example,

#include<stdio.h>

/*
Introduces the type enum Ecolor

the integer constants RED, GREEN, BLUE

*/

enum Ecolor { RED = 100, GREEN, BLUE };

int main()
{

    //colourCode is an object of enum Ecolor
    enum Ecolor colourCode = RED;

    printf("%d", colourCode);

    return 0;
}

Output: 100

 

 

Derived types in C:

The C language provides flexibility to construct any number of the derived types from the object and function types. There are follows:

Array Type:

An array type describes a contiguously allocated nonempty set of objects ( containing one or more elements). If the array element type is T, the array type is sometimes called “array of T“. Let’s understand it with an example, below I am creating an array.

int arr[5];

The element type of the above array is int, so we can call it an array of int.  Also, array types are characterized by their element type and the number of elements in the array (array size).

You can read the article, “C Arrays” for more information.

 

Structure Type:

A structure type represents a sequentially allocated nonempty set of member objects, each of which has an optionally specified name and possibly a different type.

Consider the below example of a structure;

struct Student
{
    //structure members both are complete type
    int rollno; 
    char name[10];
};

 

A structure does not contain a member with an incomplete or function type (except the flexible array) that is the reason at the time of structure declaration, it cannot contains the instance of itself but contains a pointer to itself. Consider the three examples of invalid structures in C.

struct Student
{
    //empty struct not valid in C
};

 

struct Student
{
    int a[];       //illegal
};

 

struct Student
{
    int a;
    struct Student b; // illegal
} data;

You can read more about the C structure by reading the article, structure in C.

 

Union Type:

A union type represents an overlapping nonempty set of member objects, each of which has an
optionally specified name and possibly different type.

Here is an example of a union,

union Student // Declare a simple union type
{
    int age;
    float fees;
    char name[40];
}stuInfo;

You can read more about the C structure by reading the article, union in C.

 

Function Type:

A function type describes a function with a specified return type. A function type is characterized
by its return type and the number and types of its parameters.

In any programming language, a function is a set of statements that together perform a specific task. For example below function perform the multiplication operation of given two numbers,

long long int multOfTwoNum(const int a, const int b)
{
    return (a * b);
}

 

For more detail, you can read the article, “function in C“.

 

Pointer Type:

A pointer type may be derived from a function type or an object type. A pointer type derived from the referenced type T is sometimes called “pointer to T“. Let’s understand it with an example, below I am creating a pointer.

int *ptr; //ptr is a pointer to int

There is a list of the important article which might help you.

 

Atomic Type (C11):

An atomic type describes the type designated by the construct _Atomic(type-name).

 

Recommended Post: