C sizeof Operator

Beginner
6 min read
Updated: Nov 2026
1 example

What Is sizeof Operator?

sizeof returns the size of a type or expression in bytes as a size_t. It is evaluated at compile time (except for variable length arrays) and does not evaluate its operand. Use it for portable allocation and array lengths.

Remember
sizeof(int)          /* size of a type          */
sizeof x             /* size of an object       */
sizeof arr / sizeof arr[0]   /* array length   */
printf("%zu", sizeof(x));

Cheat Sheet

Keep these facts handy while you work with sizeof Operator in C.

TopicDetail
Result typesize_t, print with %zu
Array lengthsizeof arr / sizeof arr[0]
Pointer parametersizeof p is the pointer size, not the array
mallocmalloc(n * sizeof *p)

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Operator Precedence.

Example 1 — Array length with sizeof

The element count is total bytes divided by element size.

C
#include <stdio.h>

int main(void) {
    int scores[10] = {0};
    char name[] = "C";

    printf("sizeof(int)   = %zu\n", sizeof(int));
    printf("sizeof scores = %zu\n", sizeof scores);
    printf("length        = %zu\n", sizeof scores / sizeof scores[0]);
    printf("sizeof name   = %zu\n", sizeof name);
    return 0;
}

How It Works

  1. The array holds 10 ints of 4 bytes each = 40 bytes.
  2. Dividing by sizeof scores[0] gives the element count, 10.
  3. "C" needs 2 bytes: 'C' and the terminating '\0'.
  4. Sizes assume a typical platform where int is 4 bytes.

Edge Cases & Pitfalls

Check these before relying on sizeof Operator in real programs.

param

sizeof on a parameter

Inside void f(int a[]), sizeof a is the size of a pointer, not the array. Pass the length separately.

side

Side effects are not run

sizeof(i++) does not increment i; the operand is not evaluated.

Key Takeaways

  • Portable: size things with sizeof.
  • Array length: divide total by element size.
  • Pointers: sizeof sees only the pointer.

One line: sizeof returns the size of a type or expression in bytes as a size_t.

Frequently Asked Questions

No, it is an operator evaluated at compile time. Parentheses are required only when the operand is a type name.
An array parameter is adjusted to a pointer, so sizeof gives the pointer size (8 bytes on 64-bit systems).
Continue with "C Operator Precedence" next. Know which operators run first and how ties are broken. The C Operators hub lists every topic in order.

Next: Operator Precedence

Know which operators run first and how ties are broken.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful