Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why do we use Sizeof Operator? 
3.
Requirements for  Sizeof Operator
4.
The Operand is Data Type
4.1.
Syntax
5.
The Operand is Expression
5.1.
Syntax
6.
C sizeof Example
7.
Frequently Asked Questions
7.1.
What is the return type of sizeof operator in C?
7.2.
Can we apply the sizeof operator to every expression in C?
7.3.
Can we use the sizeof operator to get the number of elements in an array?
8.
Conclusion 
Last Updated: Mar 27, 2024
Easy

operands for sizeof operator in C

Author Rhythm Jain
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

This blog article will teach you about the size and notion of operators in C. In this lesson, you will learn how to utilize the sizeof operator in your code. To understand the sizeof, we also examine several programming examples. So, first, let's define a sizeof operator.

Also see : C Static Function and  Short int in C Programming

Why do we use Sizeof Operator? 

The sizeof operator is used to return the operand's size in bytes. This operator is usually used before its operand. The operand can be either a data type or an expression. It returns a value of type size_t.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Requirements for  Sizeof Operator

All programs are generally aware of the storage capacity of all primitive data types. Even while the storage space of the data type is mostly constant throughout, it can readily vary when implemented on multiple systems. For example, we may utilize the sizeof() operator and malloc() to do dynamic array space allocation.
The result of the sizeof() operator varies greatly depending on the architecture of computers. For example, a 32-bit OS and a 64-bit OS might have vastly different results/outputs in a program for the same data type.

The behavior of the sizeof() operator changes greatly depending on the type of operand accessible to us. It might be a data type or an expression.

The Operand is Data Type

Syntax

The sizeof operator in C has the following syntax if the operand is a data variable:

sizeof(type/variable) 

The Operand is Expression

Syntax

The sizeof operator in C has the following syntax if the operand is an expression:

sizeof(expression)

Note: The sizeof operator cannot be used on expressions with function or incomplete types or on expressions that indicate a bit-field member. 

Read about Bitwise Operators in C And Floyd's Triangle in C here.

C sizeof Example

Here is a C program that shows how to use the sizeof operator in the C language:


#include <stdio.h>
int main()
{
    
    // Type
    char a;
    int b;
    float c;
    double d;


	printf("Size of char = %lu\n", sizeof(a));
	printf("Size of int = %lu\n", sizeof(b));
	printf("Size of float = %lu\n", sizeof(c));
	printf("Size of double = %lu", sizeof(d));

    //Expression
    int i = 5;
    int expression_size = sizeof(i++);
  
    // Displaying the size of the operand
    printf("\nSize of Expression = %d", expression_size);
	return 0;
}

Output

Size of char = 1
Size of int = 4
Size of float = 4
Size of double = 8
Size of Expression = 4

For practice, you can implement this code on online compiler.

Must Read Passing Arrays to Function in C

Frequently Asked Questions

What is the return type of sizeof operator in C?

The sizeof operator returns in bytes the size of its operand.

Can we apply the sizeof operator to every expression in C?

The sizeof operator cannot be used on expressions with function or incomplete types or on expressions that indicate a bit-field member.

Can we use the sizeof operator to get the number of elements in an array?

Yes we can get the size of array by using sizeof operator. 

size= sizeof(array)/ sizeof(array[0])

Conclusion 

In this article, we have extensively discussed the sizeof operator in C language. We start with a brief introduction of the sizeof operator, then discuss with an example.

Refer to our Coding Ninjas Studio Guided Path to learn Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and much more! If you want to put your coding skills to the test, check out the mock test series and enter the contests presented by Coding Ninjas Studio! But say you're just starting out and want to learn about problems posed by tech titans like Amazon, Microsoft, Uber, and so on. In such a case, for placement preparations, you must consider the challenges, interview experiences, and interview bundle.

Nonetheless, you may want to try our premium courses to give your job a competitive advantage!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Previous article
Operator Precedence and Associativity in C
Next article
Precedence of postfix ++ and prefix ++ in C
Live masterclass