Sizeof operator in C

Sizeof operator is a widely used unary operator in the embedded software development. It helps a programmer to find out the size of an operand. In other terms, it is used to determine how many bytes are allocated to hold a particular operand in the computer memory. The “sizeof” operator’s return value helps you to understand the number of bytes that are allocated in the computer memory that holds a particular data type or variable.

An operator is the one that is represented by a token or a symbol that is used to perform any operation like, addition, subtraction, multiplication, division, and so on operands (values or variables). For example, “*” is a symbol used to represent multiplication and is a binary operator. If an operator works on only one operand, such an operator is known as a unary operator like, “sizeof” operator. This always returns an unsigned integer value and will always be greater than zero.

A computer’s memory is a collection of bytes (memory units). If a “sizeof(int)” returns a value of 4, then it can be inferred that that specific computer takes 4 bytes to hold the value a variable or a value. The “sizeof” operator’s return value depends on the machine and its operating system, whether a 32-bit system or a 64-bit system. A byte may consists of 8 or more bytes depending on the computer architecture.

Syntax:

It yields the size in bytes of object representation of the type

It yields the size in bytes of the object representing the type expression if it is evaluated.

The above expressions are the constant expressions of type “ std:: size_t.”

  • Sizeof cannot be used with the function types, or incomplete types, or bit fields.
  • The result is always non-zero, even though it is applied to an empty class.
  • When it is used to a reference type, the result will be the size of the reference type.
  • When applied to a class type, the result will be the size of an object of that class plus any extra padding required to place such an object in the array.
  • A data type cannot be only a primitive data type like an integer or a floating data type, but it can also be a pointer data type and compound data like struct and union.
  • It does not evaluate the expression. Even though the expression designates a polymorphic object, the result is the size of the static type of the expression.

Eg:

Usage of the sizeof() operator can be seen, and it is applied to a cast of type “int.” The malloc() function allocates the memory and returns the pointer that will be pointing to this allocated memory. The memory space or bytes is equal to the number of bytes occupied by the data type int * 10.

Important:

  • sizeof(int) = 2 bytes
  • sizeof(signed) = 2 bytes
  • sizeof(short) = 2 bytes
  • sizeof(long) = 4 bytes
  • sizeof(char) = 1 byte
  • sizeof(‘A’) = 2 bytes //here the compiler assumes the value given is a ASCII value

//and the size will be allotted as 65 which is ASCII key of ‘A’.

  • char ch;

ch = ‘A’;

sizeof(ch) = 1 byte //ch is assigned to a character value which will be considered

                                    //as a character when compiling and hence the byte value of 1

                                    //will be assigned.

  • sizeof(double) = 8 bytes
  • sizeof(long double) = 10 bytes
  • sizeof(short double) = error

E.g:

  • Sizeof operator used in built-in types:

Output:

  • Sizeof for an array

            In this type, sizeof operator will return the –

(number of elements in the array * sizeof(array type)).

E.g:

Pin It on Pinterest

Share This