473,322 Members | 1,538 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,322 software developers and data experts.

operator ++

sam
int i=0;

printf("%d %d %d %d",++i,++i,++i,++i);

output:
4 3 2 1

why is not
1 2 3 4

I tried the code both in lcc32 and gcc both are giving same output

but for below code
printf("%d %d",p(),k());

function p() is called first in both lcc and gcc.
Nov 13 '05 #1
7 1176

"sam" <sh**********@hotmail.com> wrote in message
news:25**************************@posting.google.c om...
int i=0;

printf("%d %d %d %d",++i,++i,++i,++i);

output:
4 3 2 1

why is not
1 2 3 4

I tried the code both in lcc32 and gcc both are giving same output

but for below code
printf("%d %d",p(),k());

function p() is called first in both lcc and gcc.


because having more than one ++ operator on a varibale within the same
statement is undefined baheaviour.

int i=0;
printf("%d %d %d %d",++i,++i,++i,++i);

could print out
1) 1 1 1 1
2) 4 4 4 4
3) A N U S

or infact anything as it is undefined behaviour

Nov 13 '05 #2
sam wrote:
int i=0;

printf("%d %d %d %d",++i,++i,++i,++i);

output:
4 3 2 1

why is not
1 2 3 4

I tried the code both in lcc32 and gcc both are giving same output

but for below code
printf("%d %d",p(),k());

function p() is called first in both lcc and gcc.


In ISO-C 99, section 6.5.2, item 10:

The order of evaluation of the function designator, the actual
arguments, and subexpressions within the actual arguments is
unspecified, but there is a sequence point before the actual call.

Similarly to your post on "evaluation order" answered by others, the
evaluation order is undefined. Thus, you cannot meaningfully update a
global variable (either directly or indirectly via a function) and rely
on any evaluation order to obtain a correct result. The meaning of
undefined is explained in http://www.eskimo.com/~scs/C-faq/q11.33.html

Nov 13 '05 #3
sh**********@hotmail.com (sam) wrote:
int i=0;

printf("%d %d %d %d",++i,++i,++i,++i);


This is a newsgroup, not a chat room. You asked the very same question
two hours ago and got a perfectly good answer: RTFFAQ!

Richard
Nov 13 '05 #4
too much caffeine?
Calm down
Nov 13 '05 #5
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<bq**********@news.freedom2surf.net>...
"sam" <sh**********@hotmail.com> wrote in message
news:25**************************@posting.google.c om...
int i=0;

printf("%d %d %d %d",++i,++i,++i,++i);

output:
4 3 2 1

why is not
1 2 3 4

I tried the code both in lcc32 and gcc both are giving same output

but for below code
printf("%d %d",p(),k());

function p() is called first in both lcc and gcc.
because having more than one ++ operator on a varibale within the same
statement is undefined baheaviour.

You got the answer right but the reasoning wrong. Not in the *same
statment* but between two sequence points .. for instance in :
if(a&&b||c) ...
there is a sequence point at both the && and the || at which the side
effects of the previous part of the execution are required to be completed.
(Still this is one single statement).

int i=0;
printf("%d %d %d %d",++i,++i,++i,++i);

could print out
1) 1 1 1 1
2) 4 4 4 4
3) A N U S What intelligent undefined behaviour !!!!! ;)
or infact anything as it is undefined behaviour

Nov 13 '05 #6
Groovy hepcat sam was jivin' on 2 Dec 2003 05:12:43 -0800 in
comp.lang.c.
operator ++'s a cool scene! Dig it!
int i=0;

printf("%d %d %d %d",++i,++i,++i,++i);

output:
4 3 2 1

why is not
1 2 3 4

I tried the code both in lcc32 and gcc both are giving same output

but for below code
printf("%d %d",p(),k());

function p() is called first in both lcc and gcc.


Once again, as you have been told before, order of function argument
evaluation is unspecified and modifying an object more than once
without an intervening sequence point causes undefined behaviour.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 13 '05 #7
Groovy hepcat kikko was jivin' on Tue, 02 Dec 2003 15:53:32 GMT in
comp.lang.c.
Re: operator ++'s a cool scene! Dig it!
too much caffeine?
Calm down


What the heck are you talking about? When following up a newsgroup
post, quote (the relevant portions of) the post to which you are
replying (as I have quoted you above). Provide some context, otherwise
noone has any idea what you're on about.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 13 '05 #8

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

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
8
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.