Size of Operator in C

Size of operator in C means the sizeof operator used to find how many bytes a data type, variable, array, structure, or expression occupies in memory. It is one of the most useful operators in C because memory size matters in data handling, array processing, structure design, pointer work, dynamic memory allocation, and low-level programming.

Many beginners think sizeof is a normal function, but it is actually an operator. It helps programmers write portable code because the exact size of some types may change from one system to another. In this article, we will understand the size of operator in C, its syntax, return type, behavior with arrays and pointers, use with structures, common mistakes, and best practices.

What is Size of Operator in C?

The size of operator in C is written as sizeof. It returns the memory size, in bytes, of a type or an object.

sizeof in C tells how many bytes a type, variable, array, structure, or expression occupies in memory.

For example, if an int takes 4 bytes on your system, then sizeof(int) gives 4.

Syntax of sizeof in C

The operator can be used in two common forms.

sizeof(type)
sizeof expression

Examples:

sizeof(int)
sizeof x
sizeof(arr)
sizeof(struct Student)

Parentheses are required when a type name is used, but they are optional when a normal expression or variable is used.

Return Type of sizeof in C

The return type of sizeof is size_t. It is an unsigned integer type designed to represent sizes safely.

That is why code often prints sizeof results using %zu.

#include <stdio.h>

int main(void)
{
    printf("%zu\n", sizeof(int));
    return 0;
}

Using sizeof with Variables in C

sizeof can be used directly with variables.

int a = 10;
double b = 5.5;
char c = 'A';

printf("%zu\n", sizeof(a));
printf("%zu\n", sizeof(b));
printf("%zu\n", sizeof(c));

This gives the size of the variables based on their declared types.

Using sizeof with Data Types in C

You can use sizeof directly with built-in types as well.

TypeExample ExpressionCommon Size in Bytes
charsizeof(char)1
shortsizeof(short)2
intsizeof(int)4
longsizeof(long)4 or 8
floatsizeof(float)4
doublesizeof(double)8

These are common values, but some sizes can differ between compilers and architectures. That is one reason sizeof is useful.

sizeof with Arrays in C

When used on an array in the same scope where the array exists, sizeof returns the total size of the entire array in bytes.

int arr[5];

printf("%zu\n", sizeof(arr));
printf("%zu\n", sizeof(arr[0]));

If one int is 4 bytes, then sizeof(arr) becomes 20 and sizeof(arr[0]) becomes 4.

This makes it possible to calculate the number of elements in an array:

size_t count = sizeof(arr) / sizeof(arr[0]);

This is one of the most common and useful patterns in C.

sizeof with Pointer in C

When sizeof is used on a pointer, it returns the size of the pointer itself, not the size of the data it points to.

int x = 10;
int *ptr = &x;

printf("%zu\n", sizeof(ptr));
printf("%zu\n", sizeof(*ptr));

Here sizeof(ptr) gives the pointer size, while sizeof(*ptr) gives the size of the integer value pointed to by ptr.

ExpressionMeaning
sizeof(ptr)Size of address storage
sizeof(*ptr)Size of pointed data type

Difference Between sizeof(array) and sizeof(pointer)

This is one of the most important things to understand.

  • sizeof(array) gives the total bytes in the whole array.
  • sizeof(pointer) gives only the size of the address variable.
  • When an array is passed to a function, it usually decays into a pointer.
  • Inside that function, sizeof(array_parameter) gives pointer size, not full array size.

An array and a pointer are related in C, but sizeof clearly shows they are not the same thing.

sizeof with Structure in C

sizeof is also used with structures. The result includes structure padding added by the compiler for alignment.

struct Student
{
    char grade;
    int marks;
};

printf("%zu\n", sizeof(struct Student));

The result may be larger than the simple sum of member sizes because the compiler may insert padding bytes.

Is sizeof Evaluated at Compile Time?

In most common cases, sizeof is evaluated at compile time. That is why it is fast and very useful in constant expressions.

However, for variable length arrays, the value can be determined at runtime. So the simple rule is: usually compile time, but not in every possible case.

Practical Uses of sizeof in C

  • finding the size of data types and variables
  • calculating number of elements in an array
  • allocating memory correctly with malloc()
  • checking structure size and padding
  • writing more portable code across systems

For example, dynamic memory allocation often uses sizeof like this:

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

This is better than hardcoding a number of bytes.

Common Mistakes with sizeof in C

  • thinking sizeof is a function
  • assuming sizeof(pointer) gives the size of pointed data
  • using sizeof on array parameters inside functions and expecting full array size
  • printing sizeof result with the wrong format specifier
  • assuming all systems use the same size for every type
MistakeProblemBetter Practice
sizeof(ptr) for data lengthReturns pointer size onlyUse sizeof(*ptr) or track length separately
Using %d to print sizeofType mismatch riskUse %zu
Expecting array size inside a function parameterArray decays to pointerPass length separately

Best Practices for sizeof in C

  • Use %zu when printing sizeof results.
  • Prefer sizeof(variable) or sizeof(*ptr) in memory allocation code.
  • Use sizeof(array) / sizeof(array[0]) only where the actual array is available.
  • Do not assume type sizes are identical on every system.
  • Remember that structure size can include padding.

FAQs

What is size of operator in C?

It is the sizeof operator used to find the size of a type, variable, array, structure, or expression in bytes.

Is sizeof in C a function?

No. sizeof is an operator, not a function.

What is the return type of sizeof in C?

The return type of sizeof is size_t.

What is the difference between sizeof(array) and sizeof(pointer)?

sizeof(array) gives the total array size in bytes, while sizeof(pointer) gives only the size of the address variable.

Can sizeof be used with structures in C?

Yes. It returns the total structure size, including any compiler-added padding.

Is sizeof always evaluated at compile time?

Usually yes, but variable length arrays are an important exception where runtime evaluation can happen.