473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

About C and order of evaluation

Hello, in the program below:

#include <stdio.h>

int main(void) {
int x = 1, y = 2, z;

z = x+++y;
return 0;
}

after executing, z has the value of 3, x is 2 and y is 2.

Can someone please explain me in detail why it is treated by the
compiler as (x++)+y and not for example as x+(++y) ?
What steps the compiler does to produce that result ?

Thanks for your time and sorry for my bad english

Charalampos Pournaris

Oct 8 '07 #1
13 1566
Richard Heathfield wrote:
Thus, z = x+++y; is parsed as

[z] [=] [x] [++] [+] [y] [;]
The compiler may be happy with that code but I sure wouldn't be pleased
if it showed up in a program I had to maintain. It _looks_ like a typo,
and without analyzing the surrounding lines there's no way to tell if:

z = (x++)+y;

or

z = x+(++y);

was intended. This is another one of those instances where C
is a little too flexible (for my taste) about the syntax it will accept.
Sometimes it would be better if the compilers warned the
programmers to put in a few more spaces or parentheses so that the
meaning of the code would be unambiguous. That is unambiguous without
having to know arcane details about the how the compiler's parser
functioned.

Here's Hello World with all "extra" spaces and EOL's removed.
It's down to just 3 lines and two spaces: one between "int" and "main"
and one between "Hello" and "world". My news client wrapped this after
the second space but in the original it was only a 3 line program.

#include <stdlib.h>
#include <stdio.h>
int main(void){(void)fprintf(stdout,"Hello
world\n");(void)exit(EXIT_SUCCESS);}

This 3 line form is legal but it's also really hard to read. It would
be nice if gcc (for instance) had at least an optional -Whuman, to issue
warnings for difficult to read but otherwise legal constructs.
Regards,

David Mathog
Oct 9 '07 #2
David Mathog wrote:
Richard Heathfield wrote:
>Thus, z = x+++y; is parsed as

[z] [=] [x] [++] [+] [y] [;]

The compiler may be happy with that code but I sure wouldn't be
pleased
if it showed up in a program I had to maintain. It _looks_ like a
typo, and without analyzing the surrounding lines there's no way to
tell if:

z = (x++)+y;

or

z = x+(++y);

was intended. This is another one of those instances where C
is a little too flexible (for my taste) about the syntax it will
accept.
Sometimes it would be better if the compilers warned the
programmers to put in a few more spaces or parentheses so that the
meaning of the code would be unambiguous. That is unambiguous without
having to know arcane details about the how the compiler's parser
functioned.
It's not the compiler's job to teach the programming best coding
practices. No one prevents someone from writing clear, well formatted
source. Unfortunately too many C programmers have IOCCC ambitions.
Witness the recent thread started by Antoninus Twink complaining about
a piece of clear C code.
Here's Hello World with all "extra" spaces and EOL's removed.
It's down to just 3 lines and two spaces: one between "int" and "main"
and one between "Hello" and "world". My news client wrapped this
after the second space but in the original it was only a 3 line
program.

#include <stdlib.h>
#include <stdio.h>
int main(void){(void)fprintf(stdout,"Hello
world\n");(void)exit(EXIT_SUCCESS);}

This 3 line form is legal but it's also really hard to read. It would
be nice if gcc (for instance) had at least an optional -Whuman, to
issue warnings for difficult to read but otherwise legal constructs.
This is a subjective decision and I think, though I'm not sure, it will
be rather hard to implement. This is something that the programmer must
learn from good books and peers, not a compiler, IMHO.

Oct 9 '07 #3
David Mathog wrote:
Richard Heathfield wrote:
>Thus, z = x+++y; is parsed as

[z] [=] [x] [++] [+] [y] [;]

The compiler may be happy with that code but I sure wouldn't be pleased
if it showed up in a program I had to maintain.
"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks."

I can't find the original, only echoes in sign-quotes, alas.

--
Chris "transmogrified ducks dollin" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Oct 10 '07 #4
santosh wrote:
David Mathog wrote:
> Sometimes it would be better if the compilers warned the
programmers to put in a few more spaces or parentheses so that the
meaning of the code would be unambiguous. That is unambiguous without
having to know arcane details about the how the compiler's parser
functioned.

It's not the compiler's job to teach the programming best coding
practices. No one prevents someone from writing clear, well formatted
source. Unfortunately too many C programmers have IOCCC ambitions.
Witness the recent thread started by Antoninus Twink complaining about
a piece of clear C code.
Teach no, help with, yes. For the sake of argument, imagine that a
compiler had options like:

-Wfor_humans warn on difficult to read code
-Wstyle_fmt1 warn on source code not in specified style

Where the first would catch the "x+++y", and the second would pick up
on deviations from a specified coding style. These would be immensely
helpful, even if they have no effect whatsoever on the code generated.
Here is an example of the type of style variant I would love to be able
to flag.

if(some_condition)
do_something();

The lack of {} around the conditional code very often leads to errors
during code maintenance, and this can happen by accident when a stray
carriage return falls in the wrong place or an automatic line wrap
occurs unnoticed. Also, many projects have coding style standards, but
these are currently impossible to enforce using just the compiler. Some
projects want:
if(some_condition){
if(something_else){
}
}
else{
}

and another may demand:

if(some_condition) {
if(something_else) {
}
}
else {
}

It would be incredibly useful for me in these situations to be able to
throw a compiler switch to check for variation from these styles. The
compiler is clearly a good place to do this since it has to parse the
code thoroughly anyway, and could easily add a few more rules during
that parsing to pick up on things like indent levels, bracket spacing,
and split line if statements.

Regards,

David Mathog
Oct 10 '07 #5
David Mathog wrote On 10/10/07 12:12,:
santosh wrote:
>>David Mathog wrote:

>> Sometimes it would be better if the compilers warned the
programmers to put in a few more spaces or parentheses so that the
meaning of the code would be unambiguous. That is unambiguous without
having to know arcane details about the how the compiler's parser
functioned.

It's not the compiler's job to teach the programming best coding
practices. No one prevents someone from writing clear, well formatted
source. Unfortunately too many C programmers have IOCCC ambitions.
Witness the recent thread started by Antoninus Twink complaining about
a piece of clear C code.


Teach no, help with, yes. For the sake of argument, imagine that a
compiler had options like:

-Wfor_humans warn on difficult to read code
-Wstyle_fmt1 warn on source code not in specified style

Where the first would catch the "x+++y", and the second would pick up
on deviations from a specified coding style.
The great difficulty is in describing the "style" not
only in precise terms, but in computable terms. As an
exercise, try modifying gcc or another compiler to issue
warnings for departures from

PRE31-C. Never invoke an unsafe macro with
arguments containing assignment, increment,
decrement, or function call
-- CERT C Programming Language Secure Coding Standard
http://www.securecoding.cert.org/

(This rather rough work-in-progress was discussed on c.l.c.
in August of this year.) It's easy enough to get the compiler
to recognize the listed operations, but I think you may have
difficulty teaching it to distinguish "safe" from "unsafe"
macros.
Here is an example of the type of style variant I would love to be able
to flag.

if(some_condition)
do_something();

The lack of {} around the conditional code very often leads to errors
during code maintenance, [...]
Aside: This assertion is "very often" made, but is it
backed by measurement? I've been omitting braces from one-
line consequents of `if' and `for' and `while' for more
than three decades, and can't recall coming to grief as a
result.
[...] Also, many projects have coding style standards, but
these are currently impossible to enforce using just the compiler.
[...] The
compiler is clearly a good place to do this since it has to parse the
code thoroughly anyway, and could easily add a few more rules during
that parsing to pick up on things like indent levels, bracket spacing,
and split line if statements.
First observation: It seems to me that the formatting of
the code is among the least important things to worry about.
Badly-formatted code is hard to read, yes, but if it's truly
terrible there's no shortage of code reformatters. If you're
happy with the rearrangements reformatters can make, what's
the point of requiring that they be made by humans instead?
Save human effort for the things machines *aren't* good at.

Second observation: I once shared your view that because
the compiler has to inspect the code anyhow, the compiler is
the logical "pinch point" at which to ensure good practice.
But fifteen or twenty years ago I changed my mind: the job
of the compiler is to translate the source code, not to
enforce some organization's standards. Different standards
apply in different organizations -- even in different projects
within the same organization -- so the compiler's suite of
standards would need to be localizable, which adds to the
complexity of the compiler. It's bad enough dealing with
optimizer bugs; do you also want to handle enforcer bugs?
How much time do you want to spend debugging the scripts
or whatever that describe the local style(s)?

Software engineering is a social activity (yes, even
for the so-called "loners"), and making sure that it is
done well and/or according to standards is a social issue.
Technology influences social issues, but is seldom in and
of itself a cure for social ills.

--
Er*********@sun.com
Oct 10 '07 #6
Eric Sosman wrote:
David Mathog wrote:
.... snip ...
>
>Here is an example of the type of style variant I would love to
be able to flag.

if(some_condition)
do_something();

The lack of {} around the conditional code very often leads to
errors during code maintenance, [...]

Aside: This assertion is "very often" made, but is it backed by
measurement? I've been omitting braces from one- line consequents
of `if' and `for' and `while' for more than three decades, and
can't recall coming to grief as a result.
My personal rule is that an if controlling a one liner can be
written as a one liner. I.e. in my world:

if (whatever()) something();
and
if (whatever()) {
foo();
something();
}

are both quite legitimate. Note I do not require 0 comparison in
the conditional.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 11 '07 #7
David Mathog wrote:
>
.... snip ...
>
Here's Hello World with all "extra" spaces and EOL's removed.
It's down to just 3 lines and two spaces: one between "int" and
"main" and one between "Hello" and "world". My news client
wrapped this after the second space but in the original it was
only a 3 line program.

#include <stdlib.h>
#include <stdio.h>
int main(void){(void)fprintf(stdout,"Hello
world\n");(void)exit(EXIT_SUCCESS);}

This 3 line form is legal but it's also really hard to read. It
would be nice if gcc (for instance) had at least an optional
-Whuman, to issue warnings for difficult to read but otherwise
legal constructs.
How about:

#include <stdio.h>
int main(void){puts("Hello World);return 0;}

and, if you really insist, you can (void)puts. Expanding to
minimum readability yields:

#include <stdio.h>
int main(void) {
puts("Hello World);
return 0;
}

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 11 '07 #8
David Mathog wrote:
>
.... snip ...
>
and another may demand:

if(some_condition) {
if(something_else) {
}
}
else {
}

It would be incredibly useful for me in these situations to be able
to throw a compiler switch to check for variation from these styles.
The compiler is clearly a good place to do this since it has to
parse the code thoroughly anyway, and could easily add a few more
rules during that parsing to pick up on things like indent levels,
bracket spacing, and split line if statements.
How about:

if (!some_condition) {
/* whatever */
}
else if (something_else) {
/* whatever else */
}

which I consider clearer.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 11 '07 #9
Eric Sosman wrote:
David Mathog wrote On 10/10/07 12:12,:
>Here is an example of the type of style variant I would love to be able
to flag.

if(some_condition)
do_something();

The lack of {} around the conditional code very often leads to errors
during code maintenance, [...]

Aside: This assertion is "very often" made, but is it
backed by measurement? I've been omitting braces from one-
line consequents of `if' and `for' and `while' for more
than three decades, and can't recall coming to grief as a
result.
Measurement no, experience yes. I've seen bugs like this in other
people's code (not mine - I never uses this construct).
This one seems to be relatively common:

if(test)
do_1();
do_2();

I also recall seeing this once:

if(test)
do_1(); do_2();

In both cases resulting in subtle bugs where the code needed do_2() to
be conditional on test, whereas it actually ran unconditionally.
Beginner errors, yes, but once they are in the code, if the resulting
dysfunction isn't huge they can persist a long time.

So far I've not encountered this variant:

if(test)do(1); do_2();
Regards,

David Mathog
Oct 11 '07 #10
ch******@yahoo.com wrote:

# z = x+++y;

It's not nice to fool Mother C.

'Doctor! Doctor! It hurts when I do this!'
'Then stop doing that.'

--
SM Ryan http://www.rawbw.com/~wyrmwif/
GERBILS
GERBILS
GERBILS
Oct 19 '07 #11
"CBFalconer" <cb********@yahoo.coma écrit dans le message de news:
47***************@yahoo.com...
David Mathog wrote:
>>
... snip ...
>>
Here's Hello World with all "extra" spaces and EOL's removed.
It's down to just 3 lines and two spaces: one between "int" and
"main" and one between "Hello" and "world". My news client
wrapped this after the second space but in the original it was
only a 3 line program.

#include <stdlib.h>
#include <stdio.h>
int main(void){(void)fprintf(stdout,"Hello
world\n");(void)exit(EXIT_SUCCESS);}

This 3 line form is legal but it's also really hard to read. It
would be nice if gcc (for instance) had at least an optional
-Whuman, to issue warnings for difficult to read but otherwise
legal constructs.

How about:

#include <stdio.h>
int main(void){puts("Hello World);return 0;}

and, if you really insist, you can (void)puts. Expanding to
minimum readability yields:

#include <stdio.h>
int main(void) {
puts("Hello World);
return 0;
}
Surely you meant this Mr Falconer:

#include <stdio.h>
int main(void) {
puts("Hello World");
return 0;
}

--
Chqrlie.
Oct 21 '07 #12
Charlie Gordon wrote:
"CBFalconer" <cb********@yahoo.coma écrit dans le message de news:
47***************@yahoo.com...
>David Mathog wrote:
... snip ...
>>Here's Hello World with all "extra" spaces and EOL's removed.
It's down to just 3 lines and two spaces: one between "int" and
"main" and one between "Hello" and "world". My news client
wrapped this after the second space but in the original it was
only a 3 line program.

#include <stdlib.h>
#include <stdio.h>
int main(void){(void)fprintf(stdout,"Hello
world\n");(void)exit(EXIT_SUCCESS);}

This 3 line form is legal but it's also really hard to read. It
would be nice if gcc (for instance) had at least an optional
-Whuman, to issue warnings for difficult to read but otherwise
legal constructs.
How about:

#include <stdio.h>
int main(void){puts("Hello World);return 0;}

and, if you really insist, you can (void)puts. Expanding to
minimum readability yields:

#include <stdio.h>
int main(void) {
puts("Hello World);
return 0;
}

Surely you meant this Mr Falconer:

#include <stdio.h>
int main(void) {
puts("Hello World");
return 0;
}
Are you determined to get from #8 to at least #2
in the "Top 20 posters by number of articles"
any way you can, or are you anal by nature?

Regards,
Orlando B. Salazar


Oct 21 '07 #13
"Orlando B. Salazar" <ob*******@regenzy.neta écrit dans le message de
news: x6******************************@giganews.com...
Charlie Gordon wrote:
>"CBFalconer" <cb********@yahoo.coma écrit dans le message de news:
47***************@yahoo.com...
>>David Mathog wrote:
... snip ...
Here's Hello World with all "extra" spaces and EOL's removed.
It's down to just 3 lines and two spaces: one between "int" and
"main" and one between "Hello" and "world". My news client
wrapped this after the second space but in the original it was
only a 3 line program.

#include <stdlib.h>
#include <stdio.h>
int main(void){(void)fprintf(stdout,"Hello
world\n");(void)exit(EXIT_SUCCESS);}

This 3 line form is legal but it's also really hard to read. It
would be nice if gcc (for instance) had at least an optional
-Whuman, to issue warnings for difficult to read but otherwise
legal constructs.
How about:

#include <stdio.h>
int main(void){puts("Hello World);return 0;}

and, if you really insist, you can (void)puts. Expanding to
minimum readability yields:

#include <stdio.h>
int main(void) {
puts("Hello World);
return 0;
}

Surely you meant this Mr Falconer:

#include <stdio.h>
int main(void) {
puts("Hello World");
return 0;
}

Are you determined to get from #8 to at least #2
in the "Top 20 posters by number of articles"
any way you can, or are you anal by nature?
I guess anality rubs in on c.l.c, especially from reading posts by CBF and
RJH ;-)

--
Chqrlie.
Oct 21 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
5
by: Derek | last post by:
I came upon the idea of writting a logging class that uses a Python-ish syntax that's easy on the eyes (IMO): int x = 1; double y = 2.5; std::string z = "result"; debug = "Results:", x, y,...
1
by: Bob Hairgrove | last post by:
Why can I do this: template<typename A, typename B=A> struct X { /*...*/ }; whereas this gives me an error about an undeclared identifier with MSVC++ 7.1: struct A { A(int arg1, int...
13
by: Richard | last post by:
Boy, I'll sure bet this is a FAQ. Many years ago, my "runtime behavior of programming languages" prof absolutely guaranteed that C parameters are evaluated left-to-right. He was a bright guy...
4
by: Frank Wallingford | last post by:
Note: For those with instant reactions, this is NOT the common "why is i = i++ not defined?" question. Please read on. I came across an interesting question when talking with my colleagues....
3
by: andreas ames | last post by:
Hi all, recently I came across a line of code like the following: if seq.erase(seq.begin(), seq.end()) != seq.end() /* ... */ It made me wonder if this is just bogus or if it even can...
15
by: Jeroen | last post by:
Hi all, I've got a very specific question about the evaluation order in C++. Assume some kind of custom array class, with an overloaded subscript operator. In the following code: { my_array...
25
by: mdh | last post by:
Given the expression: while (isaspace(c = *s++)) x+1; (s is an array) Does the increment to 's' occur after "x+1" is evaluated, or after the content of s is assigned to c? Is there a...
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.