The sizeof Operator in C: Understanding Data Type Sizes

The sizeof operator in C is a powerful tool for determining the size of various data types and variables in bytes. Operating at compile time, it can be applied to any data type, including floats and pointers. This chapter discusses how the size can vary across different systems, such as 32-bit and 64-bit architectures, and highlights its importance in memory management.



The sizeof Operator in C

The sizeof operator in C is used to determine the size of a data type or variable in bytes. It operates at compile time and can be used with any data type, including floats and pointers. The size may vary depending on the system (e.g., 32-bit vs. 64-bit).

Syntax

Syntax

sizeof(type or var);

Example 1: Basic Usage

This example shows how to use sizeof with different data types:

Code

#include <stdio.h>

int main() {
    int a = 10;
    printf("Size of variable a: %d \n", sizeof(a));
    printf("Size of int: %d \n", sizeof(int));
    printf("Size of char: %d \n", sizeof(char));
    printf("Size of float: %d \n", sizeof(float));
    printf("Size of double: %d \n", sizeof(double));    
    return 0;
}
Output

Size of variable a: 4
Size of int: 4
Size of char: 1
Size of float: 4
Size of double: 8

Example 2: Using sizeof with Struct

This example shows how to use sizeof with a struct:

Code

#include <stdio.h>

struct employee {
    char name[20];
    int age;
    double salary;
};

int main() {
    struct employee e1 = {"Alice", 30, 50000.00};
    printf("Size of employee struct: %d\n", sizeof(e1));   
    return 0;
}
Output

Size of employee struct: 32

Example 3: Using sizeof with Array

This example shows the size of an array:

Code

#include <stdio.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    printf("Size of array arr: %d\n", sizeof(arr));
    return 0;
}
Output

Size of array arr: 20

Example 4: Array Length

Calculate the number of elements in an array:

Code

#include <stdio.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int length = sizeof(arr) / sizeof(arr[0]);
    printf("Number of elements in arr: %d\n", length);
    return 0;
}
Output

Number of elements in arr: 5

Example 5: sizeof in Dynamic Memory Allocation

Use sizeof with dynamic memory allocation:

Code

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = (int *)malloc(sizeof(int) * 10);
    printf("Size of dynamically allocated block: %d\n", sizeof(int) * 10);
    free(ptr);
    return 0;
}
Output

Size of dynamically allocated block: 40

Example 6: Size of Pointers

Find the size of different types of pointers:

Code

#include <stdio.h>

int main() {
    printf("Size of int pointer: %d \n", sizeof(int *));
    printf("Size of char pointer: %d \n", sizeof(char *));
    printf("Size of float pointer: %d \n", sizeof(float *));
    printf("Size of double pointer: %d \n", sizeof(double *));
    return 0;
}
Output

Size of int pointer: 8
Size of char pointer: 8
Size of float pointer: 8
Size of double pointer: 8