Miscellaneous Operators in C
C language some non-conventional operators like ternary conditional operator (?:), sizeof operator, comma operator (,), pointer operators (& and *) and member selection operators (. and ->). Here we will discuss first 3 operators while rest will be discussed later.
Conditional operator (?:) is ternary operator whose first operand is the conditional expression outcome of which will decide whether the second operand expression will be executed or the third operand expression. As an example, consider the following code snippet in which we want to assign value of x to a if and only x > y; otherwise we will assign it some other value.
x = 10;
y = 20;
a = ( x > y ) ? x : 0;
// This code will assign 0 to a
The sizeof() operator which return the number of bytes that are occupied by the operand. The operand can be a constant, data type or variable.
printf(“The size of int = %d”, sizeof(int));
// This code will display 2 on the screen
The comma operator (,) is used to link related expressions together. The expression so formed is evaluated from left-to-right and the value of right- most expression is the value of the combined expression.
x = 10;
y = 20;
z = (x = y + 1, y = x + 1, x + y);
//What is the value of z;
There are two more operators in C language; unary plus (+) and unary minus (-). Unary Plus is a unary operator which simply multiplies +1 to its operand which can be any expression. Same way unary minus multiplies -1 to its operand. These operators are used to explicitly assign some sign to the expression.