You are on page 1of 3

Some question related to ternary operator in C

Q 1. > 1. #include<stdio.h> > int main() > { > int A=4; > printf("%d",(A%2==0)?A=0:A=1); > } Minor issues: You should use "int main(void)". It's better to get into the habit of using function prototypes, even though it's not necessary. You should end main() with a "return 0;". In C99, it's no longer strictly necessary for main(), but it would be for most other functions that return a value, so it's a good idea to make a habit of it. Your format string should end with "\n". The last thing your program writes to a test file should always be a newline character, otherwise the behavior of your program is undefined. On many systems, it will cause no serious problem, but again - it's better to get into the habit of always producing it > 2. #include<stdio.h> > int main() > { > int A=4; > printf("%d",(A%2==0)?(A=0):(A=1)); > } > > > Why 1st gives error in C and 2nd program doesn't. > please explain.. Because the grammar production for a conditional-expression is (6.5.15p1): "conditional-expression: logical-OR-expression logical-OR-expression ? expression : conditional-expression" The third operand must therefore be a conditional-expression. An assignment expression such as A=1 can qualify as a conditional-express only by first being surrounded by parenthesis. Without the parenthesis, the largest portion of A=1 that can be parsed as a conditional-expression is A. As a result, (A%2==0)?A=0:A=1 gets parsed as ((A%2==0)?A=0:A) = 1 This violates the constraint that "An assignment operator shall have a modifiable lvalue as its left operand." (6.5.16p2).

Conventionally, this is described in terms of precedence of operators; so you would say that conditional operator has a higher precedence than the assignment operator. The standard, however, does not use the concept of operator precedence. One of the key reasons why it does so is because the C grammar cannot be completely described in terms of precedence. The conditional operator is the key thing that prevents it from being so described. It's the A=1 which causes the problem, and NOT the A=0, because the second operand of the conditional operator is allowed to be expression, and A=0 is an expression. Therefore, you only really need one pair of parenthesis in that expression, and the place where they're needed is not where you thought they were: A%2==0 ? A=0 :(A=1)

Q.2

I wrote the absolute function using ternary operator as follows int abs(int a) { a >=0 ? return a : return -a; } I get the following error messages if I write like this return a>=0 ? a : -a; I don't get any error. What's the difference between the two?

The second and third arguments to the ternary operator are expressions, not statements. return a is a statement

Q.3 So the operator precedence of the ternary operator in C seems truly bizarre to me. Case in point: #include <stdio.h> int main () { int i=5; int j=6; int k=7; printf("A: %d\n", i+j+(k!=7)?1:11); //prints 1 printf("B: %d\n", i+j+((k!=7)?1:11)); //prints 22 return 0; }

What is weird here? The first part is interpreted as: (11 + (k != 7)) ? 1 : 11 and the second is interpreted as 11 + ((k !=7) ? 1 :11) The first is caused by the precedence rules (binary arithmetic has higher precedence than the ternary operator) and the second circumvents the precedence rules through grouping the expression with parenthesis. Your edit asks for the reasons and one can usually only guess at those unless someone on the C committee who was present at the time comes around to help. My guess would be that it is much more common to use a complex expression and ask for its truth value than using the ternary operator to determine the value of an expression in arithmetic. Something like this comes to mind: return (froble() + 3) == 0 ? 23 : 5; // parens for sanity but works without if this would be interpreted as return (froble() + 3) == 5; I would be truly shocked.

You might also like