sizeof Operator in C

What is the sizeof Operator in C?

The sizeof operator is a compile-time unary operator that returns the size, in bytes, of its operand. The operand can be a data type or an expression. The result is of type size_t, which is an unsigned integral type defined in the <stddef.h> header.

Syntax:

sizeof(type)
sizeof expression

Note: When using sizeof with a type, parentheses are required. When using it with an expression, parentheses are optional but recommended for clarity.

Examples of Using sizeof

Example 1: Basic Data Types

#include <stdio.h>
 
int main()
{
    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 char: %zu bytes\n", sizeof(char));
    return 0;
}

Output:

advertisement
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 bytes

This C program displays the size of common data types using the sizeof operator. It prints the sizes of int, float, double, and char in bytes. The actual sizes may vary depending on the system and compiler.

Example 2: Using sizeof with Variables

#include <stdio.h>
 
int main()
{
    int number = 10;
    char letter = 'A';
    double pi = 3.14159;
 
    printf("Size of number: %zu bytes\n", sizeof(number));
    printf("Size of letter: %zu byte\n", sizeof(letter));
    printf("Size of pi: %zu bytes\n", sizeof(pi));
 
    return 0;
}

Output:

Free 30-Day Python Certification Bootcamp is Live. Join Now!
Size of number: 4 bytes
Size of letter: 1 byte
Size of pi: 8 bytes

This C program shows the size of different data types using the sizeof operator. It defines an int, a char, and a double. Then it prints their sizes in bytes: number (int), letter (char), and pi (double).

Example 3: Using sizeof with Arrays

#include <stdio.h>
 
int main()
{
    int arr[5] = {1, 2, 3, 4, 5};
 
    printf("Size of array: %zu bytes\n", sizeof(arr));
    printf("Number of elements in array: %zu\n", sizeof(arr) / sizeof(arr[0]));
 
    return 0;
}

Output:

Size of array: 20 bytes
Number of elements in array: 5

This C program shows how to find an array’s size and its number of elements. It creates an array with 5 numbers. sizeof(arr) gives the total size in bytes, and sizeof(arr[0]) gives the size of one element. Dividing them gives the total number of elements. The program prints both values and ends.

Example 4: Using sizeof with Pointers

#include <stdio.h>
 
int main() {
    int num = 10;
    int *ptr = &num;
 
    printf("Size of pointer: %zu bytes\n", sizeof(ptr));
    printf("Size of data pointed by pointer: %zu bytes\n", sizeof(*ptr));
 
    return 0;
}

Output:

advertisement
Size of pointer: 8 bytes
Size of data pointed by pointer: 4 bytes

This C program shows the size of a pointer and the size of the data it points to. It sets num = 10 and ptr as a pointer to num. sizeof(ptr) gives the size of the pointer itself, while sizeof(*ptr) gives the size of the value it points to (an int).

Common Pitfalls

1. Using sizeof on Pointers vs. Arrays

void func(int *arr) {
    printf("Size: %zu\n", sizeof(arr)); // Size of pointer, not array
}

Solution: Pass the size of the array as an additional parameter or use sentinel values.

2. sizeof and Dynamic Memory Allocation

int *arr = malloc(10 * sizeof(int));

Note: Always use sizeof(*arr) or sizeof(int) to ensure correct memory allocation.

3. sizeof and Strings

char str[] = "Hello";
printf("Size: %zu\n", sizeof(str)); // Includes null terminator

Note: sizeof includes the null terminator, whereas strlen() does not.

FAQs on sizeof Operator in C

1. What does the sizeof operator do in C?
The sizeof operator returns the size (in bytes) of a data type or variable. It helps you know how much memory a type or object takes.

2. Is sizeof evaluated at compile-time or runtime?
sizeof is evaluated at compile-time for known types and variables. This makes it very efficient.

3. What data type does sizeof return?
sizeof returns a value of type size_t, which is an unsigned integer type defined in <stddef.h> or <stdio.h>.

4. Is the size of basic data types always the same?
No. The size depends on the computer system (32-bit or 64-bit) and the compiler being used.

5. Why is sizeof useful in memory management?
It helps make sure you allocate exactly the right amount of memory, especially when using dynamic memory allocation.

6. Can sizeof be used on structures?
Yes. It gives the total size the structure occupies, including any padding added by the compiler.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.