Data Types

In the C programming language, data types are used to classify a value or a variable based on the size and type of data it can hold. A data type specifies the size of an object in the memory and the kind of data it can represent.



Following datatypes are basic datatypes in C:

In addition, there are number of qualifiers that can be applied to these basic types. The qualifiers short, long, signed and unsigned apply to integer whereas long apply to double.

d>
Data Types, Memory Size, and Descriptions
char 1 byte Stores a single character. It can hold values from -128 to 127 (signed) or 0 to 255 (unsigned).
int 4 bytes (commonly) Stores integer values. Typically ranges from -2,147,483,648 to 2,147,483,647 (signed).
float 4 bytes Stores floating-point numbers (decimals) with single precision.
double 8 bytes Stores floating-point numbers with double precision, providing more precision than float.
long 4 bytes (commonly) Stores larger integer values, often used when int is insufficient.
long long 8 bytes Stores even larger integer values than long.
short 2 bytes Stores smaller integer values. Typically ranges from -32,768 to 32,767 (signed).

Code Example: Using Different Data Types


#include <stdio.h>
int main() {
    // Basic Data Types
    printf("Size of char: %zu bytes\n", sizeof(char)); //%zu is used to print the result of 
                                                       sizeof()because it returns an
                                                        unsigned integer.                                                   
    printf("Size of int: %zu bytes\n", sizeof(int));
    printf("Size of float: %zu bytes\n", sizeof(float));
    printf("Size of double: %zu bytes\n", sizeof(double));
    printf("Size of long: %zu bytes\n", sizeof(long));
    printf("Size of long long: %zu bytes\n", sizeof(long long));
    printf("Size of short: %zu bytes\n", sizeof(short));

    // Derived Data Types
    int arr[10];
    printf("Size of array (10 elements of int): %zu bytes\n", sizeof(arr));
    printf("Size of pointer: %zu bytes\n", sizeof(int*));

    // User-Defined Data Types
    struct Person {
        char name[50];
        int age;
    };
    printf("Size of structure: %zu bytes\n", sizeof(struct Person));

    union Data {
        int i;
        float f;
        char str[20];
    };
    printf("Size of union: %zu bytes\n", sizeof(union Data));

    // Void Pointer
    printf("Size of void pointer: %zu bytes\n", sizeof(void*));

    return 0;
}

Output

Size of char: 1 bytes Size of int: 4 bytes Size of float: 4 bytes Size of double: 8 bytes Size of long: 8 bytes Size of long long: 8 bytes Size of short: 2 bytes Size of array (10 elements of int): 40 bytes Size of pointer: 8 bytes Size of structure: 54 bytes Size of union: 20 bytes Size of void pointer: 8 bytes