Code Comments
Programming Forum and web based access to our favorite programming groups.Hi Everyone,
I have the following piece of code, and i expected an error, however
i don't get an error,
int main()
{
int aa=0,b=0;
1>0?aa:b = 10;
printf("value %d %d\n",aa,b);
return(0);
}
and output is value 0 0
Thanks in advance ! ! !
Post Follow-up to this messageIn article <f840856d-216c-4bab-ac4a-f962f88a2c3a@h11g2000prf.googlegroups.co
m>,
Rahul <sam_cit@yahoo.co.in> wrote:
>Hi Everyone,
>
> I have the following piece of code, and i expected an error, however
>i don't get an error,
>
>int main()
>{
> int aa=0,b=0;
> 1>0?aa:b = 10;
> printf("value %d %d\n",aa,b);
> return(0);
>}
>
>and output is value 0 0
>
>Thanks in advance ! ! !
int main(void) is better
failure to include stdio.h
unnecessarily parenthesizing the return value
failure to indent the return statement properly
meaningless variable names
failure to capitalize "i" in text (e.g., "i don't get an error")
excessive use of exclamation points
improper capitalization of word "everyone"
use of tacky expression "Thanks in advance"
Post Follow-up to this messageRahul said:
> Hi Everyone,
>
> I have the following piece of code, and i expected an error, however
> i don't get an error,
>
> int main()
> {
> int aa=0,b=0;
> 1>0?aa:b = 10;
This is a syntax error (which therefore requires the implementation to
diagnose it as such). The syntax of the conditional operator is:
conditional-expression:
logical-OR-expression
logical-OR-expression ? expression : conditional-expression
which your code violates.
> printf("value %d %d\n",aa,b);
> return(0);
> }
>
> and output is value 0 0
>
> Thanks in advance ! ! !
Turn up your warning level. Which implementation are you using?
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Post Follow-up to this messageRichard Heathfield <rjh@see.sig.invalid> writes: > Rahul said: > > > This is a syntax error (which therefore requires the implementation to > diagnose it as such). The syntax of the conditional operator is: > > conditional-expression: > logical-OR-expression > logical-OR-expression ? expression : conditional-expression > > which your code violates. That is not really enough to show there is a syntax error. The syntax for an assignment-expression assignment-expression: conditional-expression unary-expression assignment-operator assignment-expression is more suggestive, but even then, the OP needs to know what might or might not constitute a unary-expression. It is an interesting problem to find the smallest set of grammar rules needed to show that any given piece of C is a syntax error. -- Ben.
Post Follow-up to this messageOn Mar 30, 5:19 pm, Richard Heathfield <r...@see.sig.invalid> wrote: > Rahul said: > > > > > This is a syntax error (which therefore requires the implementation to > diagnose it as such). The syntax of the conditional operator is: > > conditional-expression: > logical-OR-expression > logical-OR-expression ? expression : conditional-expression > > which your code violates. > > > > > Turn up your warning level. Which implementation are you using? > > -- > Richard Heathfield <http://www.cpax.org.uk> > Email: -http://www. +rjh@ > Google users: <http://www.cpax.org.uk/prg/writings/googly.php> > "Usenet is a strange place" - dmr 29 July 1999 I tried MS VC++ 8.0... http://www.dinkumware.com/exam/default.aspx
Post Follow-up to this messageRahul wrote, On 30/03/08 12:05:
> Hi Everyone,
>
> I have the following piece of code, and i expected an error, however
> i don't get an error,
In that case you need to tell your compiler to act as a C compiler
rather than as an extended-C-like-language compiler.
> int main()
> {
> int aa=0,b=0;
> 1>0?aa:b = 10;
This is illegal and a C compiler is required to produce a diagnostic
(warning, error or whatever).
> printf("value %d %d\n",aa,b);
Not related to your current problem, but you need to include stdio.h to
use printf.
> return(0);
> }
>
> and output is value 0 0
If you want to do this then a legal way is as follows:
#include <stdio.h>
int main()
{
int aa=0,b=0;
*(1>0?&aa:&b) = 10;
printf("value %d %d\n",aa,b);
return(0);
}
--
Flash Gordon
Post Follow-up to this messageRahul wrote:
> Hi Everyone,
>
> I have the following piece of code, and i expected an error, however
> i don't get an error,
>
> int main()
> {
> int aa=0,b=0;
> 1>0?aa:b = 10;
> printf("value %d %d\n",aa,b);
> return(0);
> }
>
> and output is value 0 0
>
> Thanks in advance ! ! !
The result of the ?: operator is not an lvalue, and cannot be assigned to.
If you want to conditionally assign to one of two objects, it's probably
clearer to write it in full:
if (1>0)
aa = 0;
else
b = 10;
If you really want unreadable code, I suppose you could do this:
int *ip[2] = {&b,&aa};
*ip[1>0] = 10;
but if I worked with you I wouldn't thank you for it :)
Philip
Post Follow-up to this message"Philip Potter" <pgp@doc.ic.ac.uk> wrote in message news:fso6hu$p5v$1@aioe.org... > Rahul wrote: > The result of the ?: operator is not an lvalue, and cannot be assigned to. > > If you want to conditionally assign to one of two objects, it's probably > clearer to write it in full: > > if (1>0) > aa = 0; > else > b = 10; > I think both these right-hand-sides should be 10. Which is the problem, repeating a possibly complex expression. But 'Flash' I think came up with simple mod of the OP's code which works and keeps the code compact. -- Bart
Post Follow-up to this messageRahul said: > On Mar 30, 5:19 pm, Richard Heathfield <r...@see.sig.invalid> wrote: > > I tried MS VC++ 8.0... I think the switches you'll need for Visual C are -W4 -Za (-W4 turns the warning level up as far as it'll go - unless they've added another level in the last few years - and -Za switches off Microsoft extensions.) -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999
Post Follow-up to this messageBartc wrote: > "Philip Potter" <pgp@doc.ic.ac.uk> wrote in message > news:fso6hu$p5v$1@aioe.org... > > > > I think both these right-hand-sides should be 10. > > Which is the problem, repeating a possibly complex expression. > > But 'Flash' I think came up with simple mod of the OP's code which works a nd > keeps the code compact. Yes, I made a typo, but I'd still write it my way. If you're worried about the complex expression being duplicated, use a temporary: int aa,b; int tmp; tmp = mycomplexexpr(); if (condition()) aa = tmp; else b = tmp;
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.