What is sizeof operator?

The sizeof operator in C and C++ is an unary operator that is used to determine the size (in bytes) of a data type or a variable. It can be applied to any data type, including fundamental types (like int, float, char, etc.), user-defined types (like structs and classes), and even arrays.

Key Points:

  • Syntax: sizeof(type) or sizeof variable
  • Returns: The size in bytes of the specified type or variable.
  • Compile-time Evaluation: The size is determined at compile time, making it efficient.
  • Varies by System: The size of data types can vary between different systems and architectures.

Example Usage:

#include <stdio.h>

int main() {
    int intVar;
    float floatVar;
    char charVar;

    printf("Size of int: %zu bytes\n", sizeof(intVar));
    printf("Size of float: %zu bytes\n", sizeof(floatVar));
    printf("Size of char: %zu bytes\n", sizeof(charVar));
    printf("Size of int type: %zu bytes\n", sizeof(int));
    return 0;
}

In this example, the sizeof operator is used to print the sizes of different data types and variables.

0 Comments

no data
Be the first to share your comment!