473,789 Members | 2,514 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
13 1595
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********@yah oo.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){(voi d)fprintf(stdou t,"Hello
world\n");(voi d)exit(EXIT_SUC CESS);}

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********@yah oo.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){(voi d)fprintf(stdou t,"Hello
world\n");(vo id)exit(EXIT_SU CCESS);}

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*******@rege nzy.neta écrit dans le message de
news: x6************* *************** **@giganews.com...
Charlie Gordon wrote:
>"CBFalconer " <cb********@yah oo.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){(voi d)fprintf(stdou t,"Hello
world\n");(v oid)exit(EXIT_S UCCESS);}

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
697
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 possibility and because the languages do not specify the order of evaluation, doing so was an error. int B::f ( int i, int j = i + 1 ) { // j defaults to i + 1
5
2574
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, z; The above example outputs:
1
1633
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 arg2=arg1); };
13
2672
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 from CMU whose research focus was on programming languages, and I believed him, but now I'm not so sure. Little help?
4
2580
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. According to the standard, most operators evaluate their operands in an unspecified order. This means that in code like this: f() + g()
3
2817
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 invoke undefined behaviour.
15
1972
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 a, b, c; a = b + c;
25
1588
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 general rule as to when this type of increment occurs? ( I understand that the increment in "c = ++*s" occurs immediately before assignment, so my guess is the same
32
3327
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 += i + j + k++;
54
3947
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 parentheses. 1) "The order of evaluation of subexpressions is determined by the precedence and grouping of operators."
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10195
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6765
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.