sizeof() operator in C | comma operator in C

Preview

Special operators

These operators are used for special purposes. There are many special operators provided by C language which are as follows:

  •  sizeof()
  •  Comma

1.  sizeof()

This operator is used to find the size of a variable, constant or data type. The size returned by this operator is in terms of bytes.

Program to demonstrate the use of sizeof() operator.

Output

(Line Wise)

#include<stdio.h>
int main()
{
int a;
float b;
char c;
printf(“\n%d”,sizeof(a));2
printf(“\n%d”,sizeof(b));4
printf(“\n%d”,sizeof(c));1
printf(“\n=%d”,sizeof(double));8
printf(“\n%d”,sizeof(long double));10
printf(“\n%d”,sizeof(short));2
printf(“\n%d”,sizeof(123));2
printf(“\n%d”,sizeof(123l));4
printf(“\n%d”,sizeof(123.45));8
printf(“\n%d”,sizeof(123.45f));4
printf(“\n%d”,sizeof(123.45L));10
return(0);
}

Description:

** In the above program, a is a variable of int type. Size of int is 2. So size of variable a would be 2.

Float type variable b is of 4 bytes.

Char type variable c is having size 1.

Similarly size of other data types  are displayed.

Sizeof(123) is 2 because 123 is int value.

Sizeof(123l) is 4 because alphabet l has been suffixed to the value.

Sizeof(123.45) is 8 as real values are by default double type values.

After suffixing f with the real value, value is converted into float type.

By suffixing L with the real value, value become long double type value.

2. Comma operator

This operator is used to separate variables from each other in variable declaration statement and to separate multiple expressions from each other in same line.

It is also used in library functions like printf() and scanf() to separate control string and arguments.

  Program to demonstrate the use of comma operator.

#include<stdio.h>
int main()
{
int a,b,c;
c=(a=5,b=4,a+b);
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
printf(“\nc=%d”,c);
return(0);
}
Output
a=5
b=4
c=9

Description:

In expression c=(a=5,b=4,a+b), a will be assigned value 5, b will be assigned value 4, then a+b will be evaluated to generate sum of 5 and 4 i.e. 9 which will be stored in variable c.


Lesson tags: comma opeartor in C, sizeof() operaor of C, special operators in c
Back to: C Programming Language