โž• C Operators
Estimated reading: 3 minutes 286 views

C sizeof Operator โ€“ Determine Memory Size in Bytes

Introduction โ€“ What Is the sizeof Operator in C?

The sizeof operator in C is used to determine the size (in bytes) of a data type or a variable. Itโ€™s a compile-time operator that returns an integer value indicating how much memory a given object or type occupies in the systemโ€™s architecture. This is particularly useful for memory management, portability, and system-level programming.

In this guide, youโ€™ll learn:

  • How the sizeof operator works
  • Syntax for using sizeof with types and variables
  • Practical examples and use cases
  • Differences on 32-bit vs 64-bit systems

Syntax of sizeof

sizeof(type)
sizeof variable

Both forms are valid. When used with a type, it requires parentheses. When used with a variable, parentheses are optional.

Examples:

sizeof(int)       // size of int type
sizeof x          // size of variable x
sizeof(char[10])  // size of an array (10 bytes)

Examples of sizeof in Action

#include <stdio.h>

int main() {
    int a = 10;
    double d = 3.14;
    char ch = 'A';
    float arr[5];

    printf("Size of int: %lu bytes\n", sizeof(a));
    printf("Size of double: %lu bytes\n", sizeof(d));
    printf("Size of char: %lu bytes\n", sizeof(ch));
    printf("Size of array: %lu bytes\n", sizeof(arr));
    return 0;
}

Output (on a 64-bit system):

Size of int: 4 bytes  
Size of double: 8 bytes  
Size of char: 1 byte  
Size of array: 20 bytes  

Important Facts About sizeof

  • sizeof is evaluated at compile time, not runtime
  • It works for both primitive types and user-defined types
  • Arrays return the total size: sizeof(arr) = element size ร— number of elements
  • For pointer types, sizeof(ptr) returns size of the pointer (not the data it points to)
  • Size varies depending on system architecture (32-bit vs 64-bit)

Common Mistakes and Tips

MistakeProblemFix
sizeof(*ptr) vs sizeof(ptr)*ptr returns size of data; ptr returns pointer sizeChoose based on what you need
sizeof("Hello")Returns 6, not 5 (includes \0 null terminator)Use strlen() if excluding \0
Assuming same size across platformsSizes of types like int can varyUse sizeof() for portability

sizeof with Data Types (Typical Sizes on 64-bit Systems)

Data TypeSize (bytes)
char1
int4
float4
double8
long8
short2
pointer8

Use sizeof() instead of hardcoding values for better portability.


Summary โ€“ Recap & Next Steps

The sizeof operator is an essential tool for inspecting data size and writing platform-independent code. It ensures that programs allocate the correct amount of memory, work well across different systems, and avoid undefined behavior.

Key Takeaways:

  • sizeof returns the number of bytes used by a variable or data type
  • It works on primitive types, structures, arrays, and pointers
  • Use it for memory management, array bounds, and portability
  • Parentheses are required for types, optional for variables

Real-World Relevance:

sizeof is widely used in embedded systems, memory allocation (malloc()), serialization, file I/O, and when building portable software.


Frequently Asked Questions (FAQ)

Is sizeof evaluated at compile time?

Yes. For fixed-size types and variables, sizeof is resolved during compile time, which improves performance.


What is the difference between sizeof(x) and sizeof(x + y)?

sizeof(x) returns the size of variable x.
sizeof(x + y) returns the size of the result of the expression x + y, based on type promotion.


What does sizeof("Hello") return?

It returns 6, not 5, because string literals include a null terminator \0.


Does sizeof return value in bits or bytes?

sizeof returns the size in bytes. Use * 8 if you need the size in bits.


Can I use sizeof on arrays and pointers?

Yes. But note:

  • sizeof(array) gives total size (number of elements ร— element size)
  • sizeof(pointer) gives pointer size (usually 4 or 8 bytes)

Share Now :
Share

๐Ÿ“ C sizeof Operator

Or Copy Link

CONTENTS
Scroll to Top