Skip to content
Related Articles
Open in App
Not now

Related Articles

sizeof operator in C

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 18 Jan, 2023
Improve Article
Save Article

Sizeof is a much-used operator in the C. It is a compile-time unary operator which can be used to compute the size of its operand. The result of sizeof is of the unsigned integral type which is usually denoted by size_t. sizeof can be applied to any data type, including primitive types such as integer and floating-point types, pointer types, or compound datatypes such as Structure, union, etc.

Usage of sizeof() operator 

sizeof() operator is used in different ways according to the operand type. 

1. When the operand is a Data Type: When sizeof() is used with the data types such as int, float, char… etc it simply returns the amount of memory allocated to that data types. 

Example:

C




// C Program To demonstrate
// Size of operators
#include <stdio.h>
int main()
{
    printf("%lu\n", sizeof(char));
    printf("%lu\n", sizeof(int));
    printf("%lu\n", sizeof(float));
    printf("%lu", sizeof(double));
    return 0;
}


Output

1
4
4
8

Note: sizeof() may give different output according to machine, we have run our program on a 32-bit gcc compiler.

2. When the operand is an expression: When sizeof() is used with the expression, it returns the size of the expression. 

Example:

C




// C Program To demonstrate
// operand as expression
#include <stdio.h>
int main()
{
    int a = 0;
    double d = 10.21;
    printf("%lu", sizeof(a + d));
    return 0;
}


Output

8

As we know from the first case size of int and double is 4 and 8 respectively, a is int variable while d is a double variable. The final result will be double, Hence the output of our program is 8 bytes.

Type of operator

sizeof() is a compile-time operator. compile time refers to the time at which the source code is converted to a binary code. It doesn’t execute (run) the code inside (). 

Example:

C




// C Program to illustrate
// that the 'sizeof' operator
// is a 'compile time operator'
#include <stdio.h>
 
int main(void)
{
    int y;
    int x = 11;
 
    // value of x doesn't change
    y = sizeof(x++);
 
    // prints 4 and 11
    printf("%i %i", y, x);
 
    return (0);
}


Output

4 11

If we try to increment the value of x, it remains the same. This is because x is incremented inside the parentheses and sizeof() is a compile-time operator.  

Need of Sizeof 

1. To find out the number of elements in an array: Sizeof can be used to calculate the number of elements of the array automatically. 

Example:

C




// C Program
// demonstrate the method
// to find the number of elements
// in an array
#include <stdio.h>
int main()
{
    int arr[] = { 1, 2, 3, 4, 7, 98, 0, 12, 35, 99, 14 };
    printf("Number of elements:%lu ",
           sizeof(arr) / sizeof(arr[0]));
    return 0;
}


Output

Number of elements:11 

2. To allocate a block of memory dynamically: sizeof is greatly used in dynamic memory allocation. For example, if we want to allocate memory that is sufficient to hold 10 integers and we don’t know the sizeof(int) in that particular machine. We can allocate with the help of sizeof. 

Syntax:

int* ptr = (int*)malloc(10 * sizeof(int));

For more information, refer to the article – Allocate a Block of Memory Dynamically.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!