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. and NOT the A=0. What's the difference between the two? The second and third arguments to the ternary operator are expressions. return a is a statement . Therefore.2 I wrote the absolute function using ternary operator as follows int abs(int a) { a >=0 ? return a : return -a. because the second operand of the conditional operator is allowed to be expression. and A=0 is an expression. The conditional operator is the key thing that prevents it from being so described. however. so you would say that conditional operator has a higher precedence than the assignment operator. I don't get any error. and the place where they're needed is not where you thought they were: A%2==0 ? A=0 :(A=1) Q. The standard. One of the key reasons why it does so is because the C grammar cannot be completely described in terms of precedence. } I get the following error messages if I write like this return a>=0 ? a : -a. you only really need one pair of parenthesis in that expression. It's the A=1 which causes the problem. does not use the concept of operator precedence. not statements.

int k=7.h> int main () { int i=5.3 So the operator precedence of the ternary operator in C seems truly bizarre to me. i+j+(k!=7)?1:11). } 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. Something like this comes to mind: return (froble() + 3) == 0 ? 23 : 5.Q. //prints 22 return 0. . printf("A: %d\n". i+j+((k!=7)?1:11)). 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. int j=6. Case in point: #include <stdio. I would be truly shocked. //prints 1 printf("B: %d\n". 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. // parens for sanity but works without if this would be interpreted as return (froble() + 3) == 5.