conditional operator

B

bauran

1. #include<stdio.h>
int main()
{
int A=4;
printf("%d",(A%2==0)?A=0:A=1);
}

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..
 
C

Coos Haak

Op Sun, 18 Oct 2009 09:49:55 -0700 (PDT) schreef bauran:
1. #include<stdio.h>
int main()
{
int A=4;
printf("%d",(A%2==0)?A=0:A=1);
}

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..

What does the compiler say?
 
J

James Kuyper

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)
 
B

bauran

Thanks james kuyper for your response but please can you tell me why
this code compiles correctly in c++

#include<stdio.h>
int main(void)
{
int A=4;
printf("%d",(A%2==0)?A=0:A=1);
printf("\n");
}
 
B

Ben Bacarisse

bauran said:
Thanks james kuyper for your response but please can you tell me why
this code compiles correctly in c++

#include<stdio.h>
int main(void)
{
int A=4;
printf("%d",(A%2==0)?A=0:A=1);
printf("\n");
}

That's really a question that belongs in comp.lang.c++ but there is
some overlap simply because C and C++ are different in this regard.

The grammar for C says:

conditional-expression:
logical-OR-expression
logical-OR-expression ? expression : conditional-expression

but for C++ it says:

conditional-expression:
logical-or-expression
logical-or-expression ? expression : assignment-expression

As a result, your expression is pared as:

((A % 2) == 0) ? (A = 0) : (A = 1)

in C++.

Interestingly, another difference between the two languages means that
even if you put the brackets in to force the expression to be parsed
as it is in C:

(((A % 2) == 0) ? (A = 0) : A) = 1

the result is still valid because the result of this conditional
expression is an lvalue (roughly an assignable expression) in C++.

As others have pointed out, you wouldn't write this expression like
this in either language.
 
K

Kenny McCormack

It's called a thread. Please don't corrupt threads with your typically
pedantic net policing.

They've changed their basic tune lately. It used to be clearly intended
as "go away - just go away - we don't want your kind here". If you go
back 2, 5, 10 years in Google and check, you'll see.

Now, no doubt in response to their critics, they've softened it, so that
they are actually pretending that they care and that they are only
looking out for the OPs best interests - and that the OP should actually
be grateful for the redirect.

The problem, of course, which they are either willfully ignoring, or,
giving them the benefit of the doubt, their autism/Aspergers prevents
them from seeing, is that if you're looking for an answer, a redirect is
basically a "send to /dev/null" response. Certainly, no one is going to
be "grateful" for it. Further aggravating the problem is that the regs
often say "I know the answer, but I can't tell you here". Needless to
say, the typical OP is not amused...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top