Social Icons

10 July 2012

0 The Conditiona operator in C

C ternary operator is sometimes called conditional operator or ternary operator because it is a shorthand of combination of the if-else and return statement and has three operands. The syntax of C ternary operator is as follows:

 Syntax:
condition ? expression 1 : expression 2
C first evaluates the condition. Based on the return value of the condition the second or third operand is evaluated:
  • If the condition is evaluated to true (1), only the second operand (expresion 1) is evaluated. Then the operator return the value of the expression 1.
  • If the condtion is evaluated to false (0), only the third operand (expression 2) is evaluated. The value of the expression 2 is returned.
Example:
/*Demonstration of Conditional Operator */
#include<stdio.h>
int main()
{
 int i,j,big;
printf("Enter values of i and j\n");
scanf("%d%d",&i&j);
big=(i>j)?i:j;
printf("Bigger number is %d",big);
return 0;
}


0 comments:

Post a Comment

Hey this is a test comment

Related Posts Plugin for WordPress, Blogger...