- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Illegal piece of code (conditional operator)
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-27-2003 06:58 AM
тАО03-27-2003 06:58 AM
Illegal piece of code (conditional operator)
I've seen this code snippet before:
int main()
{
int x = 10;
int y = 80;
int z = 110;
x == 10 ? y = 20 : z = 40;
printf("x = %d, y = %d, z = %d\n", x, y, z);
}
Which I think it's illegal, because of the order of precedence...
I think that analyzing this expression:
x == 10 ? y = 20 : z = 40;
produces the following implied parenthesized evaluation.
I mean it's evaluated as if I had written the expression like this:
(x == 10 ? y = 20 : z)= 40
in the first place...
And as one can see the left hand side of this assignment expression is not an
lvalue (the result of this conditional expression is not an lvalue, is a value).
I think what would be legal is to have written this expression like this in
the first place:
x == 10 ? (y = 20 ): (y= 40);
Am I right with my findings??
Additionally I also think that this expression:
x == 10 ? (y = 20 ): (z = 40);
Written with only a set of parentheses in the 3rd operand
is also legal in ANSI C. I mean:
x == 10 ? y = 20: (z= 40); /* LEGAL */
x == 10 ? y = 20: z = 40; /* ILLEGAL */
I mean only the last set of parentheses is required (I think...)
Maybe, because the second operand 'y = 20'
is embedded within '?' and ':'
and then in that
case there's no ambiguity.
Any input will be highly appreciated...
Thank you very much in advance...
Max
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-27-2003 07:08 AM
тАО03-27-2003 07:08 AM
Re: Illegal piece of code (conditional operator)
why not to try? I believe it will work even without brackets. This is conditional operator, and everything before ? is treated as a condition (false/true), following text between ? and : or ; is treated as should be executed when condition is true and following text between : and ; is executed if condition is false
Eugeny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-27-2003 07:11 AM
тАО03-27-2003 07:11 AM
Re: Illegal piece of code (conditional operator)
seems legal to me.
If X is equal to 10, then set y to equal 20; otherwise set z equal to 40
Sound kosher to me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-27-2003 07:13 AM
тАО03-27-2003 07:13 AM
Re: Illegal piece of code (conditional operator)
Eugeny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-27-2003 07:15 AM
тАО03-27-2003 07:15 AM
Re: Illegal piece of code (conditional operator)
Eugeny