sizeof Operator in C

In this blog post, you will learn the sizeof Operator in C and their concept. You will learn how to use sizeof operator in your code. We also see some programming examples to understand the sizeof. So let’s understand what is a sizeof operator.

 

What is the sizeof Operator in C?

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. sizeof operators return a value of type size_t (an unsigned integer type) and the value of the result is implementation-defined.

The sizeof operator helps you to find the amount of storage, in bytes, required to store an object of type T. Also, it allows you to avoid specifying machine-dependent data sizes in your programs.

 

sizeof Syntax:

sizeof(type)	(1)	


sizeof expression (2)

1. type - Any type name. Returns the size in bytes for type name. for example, char, int, float,.. etc.

2. expression - Returns the size in bytes for the expression.

 

Note: The sizeof operator can not be applied to an expression that has a function type or an incomplete type,  or to an expression that designates a bit-field member.

 

Now let’s see a C program to find the size of int, float, double, and char using the sizeof operator. In the below example, you will learn to evaluate the size of each variable using the sizeof.

#include<stdio.h>

int main()
{
    //variables
    char a;
    int b;
    float c;
    double d;


    // sizeof evaluates the size of a variable
    printf("Size of char in bytes: %zu\n", sizeof(a));
    printf("Size of int in bytes: %zu\n", sizeof(b));
    printf("Size of float in bytes: %zu\n", sizeof(c));
    printf("Size of double in bytes: %zu\n", sizeof(d));

    return 0;
}

Output:

sizeof operator in c

 

Important points related to sizeof operator in C:

1. The sizeof operator returns the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type.

 

2. The sizeof operator can not be applied to an expression that has a function type or an incomplete type,  or to an expression that designates a bit-field member. Let’s see an example,

// Compile with /std:c11
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>

typedef struct
{
    uint32_t a:1;
    uint32_t b:1;
    uint32_t c:30;
} Sbitfield;

int main()
{

    Sbitfield data;

    // bit field bytes
    printf("sizeof(data.a) = %d\n", sizeof(data.a));

    return 0;
}

Output:

error: 'sizeof' applied to a bit-field|

 

3. When sizeof is applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding. Let’s see an example,

// Compile with /std:c11
#include <stdalign.h>
#include <stdio.h>
#include <stddef.h>

typedef struct
{
    char a;
    int b;
    float c;
    double d;
} data;

int main()
{

    //total number of bytes including padding bytes
    printf("sizeof(data) = %d\n", sizeof(data));

    return 0;
}

Output:

sizeof(data) = 24

 

4. If the type of the operand is a variable-length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant. Let’s see some examples,

a.)  sizeof with VLA

// Compile with /std:c11
#include <stdalign.h>
#include <stdio.h>
#include <stddef.h>

int main()
{
    const int n = 3;

    char b[n+3]; // variable length array

    printf("%zu\n",sizeof b);

    return 0;
}

Output: 6

 

b.)  sizeof with operand which is not evaluated.

// Compile with /std:c11
#include <stdalign.h>
#include <stdio.h>
#include <stddef.h>

int main()
{

    int a = 27;

    int b = sizeof(a++); //value of a doesn't change

    printf("a = %u\n", a);

    printf("b = %u\n", b);

    return 0;
}

Output:

a = 27

b = 4

 

Application of sizeof operator in C:

Let’s see some uses of sizeof in C programming with some example code.

Use-1

Suppose you need to allocate memory for the 100’s of integers. Assume that the size of an integer is 4 bytes on your platform. So we can allocate the memory using the below expression.

//Where 4 is size of int

int *ptr = malloc(100 *4);

But the problem arises when you will port your code for a machine where the integer size is 2 bytes. We know that size of  int varies among machines. We can resolve this problem by using the sizeof as an argument to the malloc function. See the below expression.

//allocate memory for the 100's integers

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

 

Use 2:

Sizeof can be used to calculate the number of elements of the array. Suppose you have an integer array and it has been initialized with elements.

int arr[] = { 10, 12, 43, 4, 47, 98, 0, 27, 35, 7, 16};

The number of integers is the number of elements in the array but is not specified. It is easy to determine the number of integers by using the sizeof operator to calculate the number of elements in the array.

const int arraySize = ( sizeof arr) / ( sizeof arr[0] );

The const integer value arraySize is initialized from the number of array elements.

 

 

Recommended Post:

Leave a Reply

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