Misc Operators ↦ sizeof & ternary

Misc Operators ↦ sizeof & ternary
There are few other important operators including sizeof and ? : supported by C Language.

OperatorDescriptionExample
sizeof()Returns the size of an variable.sizeof(a), where a is integer, will return 4.
&Returns the address of an variable.&a; will give actual address of the variable.
*Pointer to a variable.*a; will pointer to a variable.
? :Conditional ExpressionIf Condition is true ? Then value X : Otherwise value Y


Example
Try following example to understand all the miscellaneous operators available in C programming language:

#include <stdio.h>

main
()
{
int a = 4;
short b;
double c;
int* ptr;

/* example of sizeof operator */
printf
("Line 1 - Size of variable a = %d\n", sizeof(a) );
printf
("Line 2 - Size of variable b = %d\n", sizeof(b) );
printf
("Line 3 - Size of variable c= %d\n", sizeof(c) );

/* example of & and * operators */
ptr
= &a; /* 'ptr' now contains the address of 'a'*/
printf
("value of a is %d\n", a);
printf
("*ptr is %d.\n", *ptr);

/* example of ternary operator */
a
= 10;
b
= (a == 1) ? 20: 30;
printf
( "Value of b is %d\n", b );

b
= (a == 10) ? 20: 30;
printf
( "Value of b is %d\n", b );
}

When you compile and execute the above program it produces following result:

value of a is  4
*ptr is 4.
Value of b is 30
Value of b is 20


Post a Comment