C sizeof Operator

C sizeof Operator takes a single operand and returns its size in memory in bytes.

Syntax

The syntax to find the size of an operand x using sizeof operator is

sizeof x
ADVERTISEMENT

Examples

In the following example, we take an integer in a, and find the size of a using sizeof operator.

main.c

#include <stdio.h>

int main() {
    int a = 54;
    int size = sizeof a;
    printf("Size : %d\n", size);
    return 0;
}

Output

Size : 4
Program ended with exit code: 0

In the following example, we take an integer array in arr, and find the size of this array in memory using sizeof operator.

main.c

#include <stdio.h>

int main() {
    int arr[] = {2, 4, 6, 8};
    int size = sizeof arr;
    printf("Size : %d\n", size);
    return 0;
}

Output

Size : 16
Program ended with exit code: 0

Since the array contains four integers, and each of integer in this machine occupies four bytes, the size of array is 16 bytes.

Conclusion

In this C Tutorial, we learned about sizeof operator and how to use this operator to find the size of given variable of any operand in the memory.