Program for Sizeof Operator in C

C Programming : Sizeof Operator

The sizeof operator in C programming is used to determine the amount of space or memory any object/operand is taking.It is a kind of unary operator which works at compile time. It returns the memory occupied in terms of bytes .

sizeof operator in c

Sizeof Operator

While using the sizeof operator in C programming, always “%lu” or “%zu” format specifier is used. We can use the sizeof operator with normal format specifiers also and no error will be generated but the correct way to use sizeof operator is using %lu or %zu format speciffier.

This sizeof operator can be used to calculate the size of a variable as well as of a data type also.

Syntax 

sizeof(variable);

 The above mentioned syntax is used to calculate the size of a variable.

Sizeof(data_type);

 The above mentioned syntax is used to calculate the size of a datatype.

Example 1:

Run

#include<stdio.h>
int main ()
{
  int a = 0;
  double d = 17.71;
  printf ("%lu", sizeof (a + d));
  return 0;
}

Output:

8 

Example 2:

Run

#include<stdio.h> 
int main() {
int a = 16;
   printf("Size of variable a : %lu\n",sizeof(a));
   printf("Size of int data type : %lu\n",sizeof(int));
   printf("Size of char data type : %lu\n",sizeof(char));
   printf("Size of float data type : %lu\n",sizeof(float));
   printf("Size of double data type : %lu\n",sizeof(double));
   return 0;
}

Output:

Size of variable a : 4
Size of int data type : 4
Size of char data type : 1
Size of float data type : 4
Size of double data type : 8

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription