The conditional operators ? and : are sometimes called ternary operators since they take three arguments.
Their general form is,
if
This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y.
The limitation of the conditional operators is that after the ? or after the : only one C statement can occur. In practice rarely is this the requirement.
Their general form is,
expression 1 ? expression 2 : expression 3
if
expression 1
is true (that is, if its value is non-zero), then the value returned will be expression 2
, otherwise the value returned will be expression 3
#include <stdio.h>
int main()
{
int x, y ;
scanf ( "Enter a number: %d", &x );
y = ( x > 5 ? 3 : 4 );
printf("Value of y is %d\n", y );
return 0;
}
This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y.
The limitation of the conditional operators is that after the ? or after the : only one C statement can occur. In practice rarely is this the requirement.
Related topics:
Overview of Operators in C | Arithmetic Operators in C | Relational Operators in C | Logical Operators in C | Bitwise Operators in C | Compound Assignment Operators in C | Miscellaneous Operators in C | Operator Precedence and Associativity in C
List of topics: C Programming
No comments:
Post a Comment