bauran said:
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)