Miscellaneous operators

Except for Arithmetic and Logical Operator, there are some special Operators provided by C language which performs special operations and description of them summarized in the following table.

 

Operator

Description

Example

sizeof()

Yields the size of a specified variable

sizeof(a), where a is

the integer will return 4.

&

Returns the address of a variable

&a; returns the actual

address of the variable.

*

Pointer to a variable

*a;

?:

Conditional Expression

If Condition is true ?

then value X :

otherwise value Y

 

1. Write a Program to perform operation of Miscellaneous Operators

/*A C Program for Misc. Operators*/

#include <stdio.h>

void main()

{

int num1 = 4;

short num2;

double num3;

int*ptr;


/* example of sizeof operator */

printf("Line 1 - Size of variable num1 = %zu\n", sizeof(num1) );

printf("Line 2 - Size of variable num2 = %zu\n", sizeof(num2) );

printf("Line 3 - Size of variable num3= %zu\n", sizeof(num3) );


/* example of & and * operators */

ptr = &num1; /* 'ptr' now contains the address of 'a'*/

printf("Value of num1 is %d\n", num1);

printf("*ptr is %d.\n", *ptr);


/* example of ternary operator */

num1 = 10;

num2 = (num1 == 1) ? 20: 30;

printf( "Value of num2 is %d\n", num2 );

num2 = (num1 == 10) ? 20: 30;

printf( "Value of num2 is %d\n", num2 );

}


Output: