โž• C Operators
Estimated reading: 3 minutes 7 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ“ C sizeof Operator

Or Copy Link

CONTENTS
Scroll to Top