473,322 Members | 1,510 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.

post and pre increment

2
what is difference between
1. y=++x++;
2. y=(++x)++;
3. y=++(x++);
Jan 10 '09 #1
25 4381
JosAH
11,448 Expert 8TB
@jyots
Have you tried to compile your examples? If so, you would've noticed that none of them compile. A ++ operator (pre- and post) takes an lvalue as its operand and its result is an rvalue so you can't apply two of them in a row.

kind regards,

Jos
Jan 10 '09 #2
jyots
2
y=(++x)++;

this statement is givin no problem... but rest of them are givin error..
Jan 10 '09 #3
JosAH
11,448 Expert 8TB
@jyots
That expression shouldn't compile either; if your compiler accepts it throw your compiler away and install a decent one. This is what the Standard has to say about it:

[#2] Except when it is the operand of the sizeof operator,
the unary & operator, the ++ operator, the -- operator, or
the left operand of the . operator or an assignment
operator, an lvalue that does not have array type is
converted to the value stored in the designated object (and
is no longer an lvalue)
. If the lvalue has qualified type,
the value has the unqualified version of the type of the
lvalue; otherwise, the value has the type of the lvalue. If
the lvalue has an incomplete type and does not have array
kind regards,

Jos
Jan 10 '09 #4
YarrOfDoom
1,247 Expert 1GB
Same here on bcc32, got any suggestions on another (freeware) compiler to use?
Jan 10 '09 #5
Download codeblocks it is free and update IDE and also come with mingw compiler
Jan 10 '09 #6
YarrOfDoom
1,247 Expert 1GB
Tried it, with the same results. Are you sure about this Jos, because now you've already declared 2 quite well known compilers (as far as I know anything about it) to be not good.
Jan 10 '09 #7
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. y=++x++;       //ERROR
  2. y=(++x)++;     //OK
  3. y=++(x++);     //ERROR
  4.  
The postfix increment has a higher precedence so it goes first. The result of an x++ is not an l_value. That means you can't use it implicitly as an assignment. For that reason this code is also no good:
Expand|Select|Wrap|Line Numbers
  1. x++ = y;   //ERROR.
  2.  
However, y= y=(++x)++ is OK because the result of ++x is an l_value:
Expand|Select|Wrap|Line Numbers
  1. ++x = y;  //OK. 
  2.  
The reason for the difference is that the prefix operator actually changes the object whereas the postfix operator changes only a copy and that copy is not to be used as an l_value.

If your compiler rejects three of the examples, get a new compiler. Only the second example should compile.
Jan 10 '09 #8
Tassos Souris
152 100+
@weaknessforcats

No this is not correct:
Expand|Select|Wrap|Line Numbers
  1. ++x = y;
  • The result of a prefix incrementation operator is not an lvalue.
  • Postfix incrementation does not affect a copy of the operand but the operand itself. The difference, from the prefix incrementation, is the timing that the value is noted. As far as postfix incrementation is concerned, the value gets noted first and then the operand is incremented (++) or decremented (--) by 1.
Jan 10 '09 #9
YarrOfDoom
1,247 Expert 1GB
@Tassos Souris
It compiles without a problem though.
But if you try:
Expand|Select|Wrap|Line Numbers
  1. int x = 0;
  2. int y = 0;
  3. ++x = x;
  4. ++y = y+5;
  5. cout << x << " " << y;
Output when compiled with bcc32: 1 5
Output when compiled with mingw: 1 6
So which one is right, or is neither of them?
Jan 10 '09 #10
Tassos Souris
152 100+
Neither of them is correct. As Jos said the result is not a lvalue.
Visual Studio gives (correctly) a compile error:
Expand|Select|Wrap|Line Numbers
  1. error C2106: '=' : left operand must be l-value
  2.  
For example, consider this:
Expand|Select|Wrap|Line Numbers
  1. int x[ 2 ];
  2. int *ptr = x;
  3.  
This is not correct:
Expand|Select|Wrap|Line Numbers
  1. ++*ptr = 0;
  2.  
That is, because the 'result' of the assignment expression depends on the ++ operator. The zero (0) will be placed after the ++ has been 'resolved'. Of course, ++ does not produce a lvalue so a compile time error must be given by the compiler.

However, these are correct
Expand|Select|Wrap|Line Numbers
  1. *++ptr = 0;
  2. *ptr++ = 0;
  3.  
Because the 'result' of the assignment "does not depend" on the ++ operator.
Let's examine the first case:
Expand|Select|Wrap|Line Numbers
  1. *++ptr = 0;
... the ++ does not produce a lvalue but then the * operator takes effect so everything is ok
Expand|Select|Wrap|Line Numbers
  1. *ptr++ = 0;
... the value is noted first, * takes place and then the ++ is activated (which has no effect on the assignment).

So the problem is not where the ++ or -- are: on the left or on the right of the = operator. But, if the result that they do not produce a lvalue actually affects the assignment.
Jan 10 '09 #11
newb16
687 512MB
@Tassos Souris
Jos said "...Except when it is the operand of the sizeof operator,
the unary & operator, the ++ operator..."

And i can quote(retype, as it's secured) 5.3.2.1 of the 14822 standard, regarding prefix ++/-- operator:
"The value is the new value of the operand; it is an lvalue"
You can
Expand|Select|Wrap|Line Numbers
  1.  printf("%p\n", &a );
  2.  printf("%p\n", &++a );
And it should print the same.
Jan 10 '09 #12
Tassos Souris
152 100+
@newb16
Expand|Select|Wrap|Line Numbers
  1. &++a
  2.  
correct result:

Expand|Select|Wrap|Line Numbers
  1. '&' requires l-value
  2.  
++ on a does not produce a lvalue so the use of &++a is illegal.

And this is of course logical. What does ++a do? It increments 'a' and returns back the new value.. why do you want to get the address of that new value?? this is both illegal and not logical...

And is exactly what Jos stated...
Common English indicate that the expression Except when means 'every other case except this'. When used other than as a operand to the sizeof() operator (that means in all cases; including our examples) it does not produce an lvalue.

This is legal:
Expand|Select|Wrap|Line Numbers
  1. int y = 0;
  2. size_t size = sizeof( ++y );
  3.  
, and also, since the operand of the sizeof() operator is not evaluated if it is not a variable length array type, y remains 0.
Jan 10 '09 #13
newb16
687 512MB
@Tassos Souris
Compiler version, please. If it works contrary to what 5.3.2.1 of the 14822 standard says regarding prefix ++/-- operator:
"The value is the new value of the operand; it is an lvalue"

It works on old dos turbo c and on gcc 3.3.3 from cygwin.

And this is of course logical. What does ++a do? It increments 'a' and returns back the new value.. why do you want to get the address of that new value?? this is both illegal and not logical...
Not only new value, but the variable itself that holds the new value. This is not the case of postfix one, when the old value is returned and the variable itself holds the new value.
Jan 11 '09 #14
newb16
687 512MB
@YarrOfDoom
The result of this expression is undefined, because compiler can add 5 to old value, increment y in left part, and then overwrite it with 5, or increment first and then calculate 1+5 and then assign; It happns because the only 'sequence point' here is at the end of the expression.
Jan 11 '09 #15
JosAH
11,448 Expert 8TB
There's a difference between C and C++ here; I assumed C was being used by the OP and quoted the C Standard text. In C none of the expressions compile because none of the (pre and post) increment operators result in an lvalue.

C++ however treats both operators differently: the preincrement ++ operator seems to leave an lvalue; the postincrement ++ operator doesn't. While I agree that my assumption could've been totally wrong (about the language being used), the OP should've said which one actually was used.

kind regards,

Jos
Jan 11 '09 #16
Tassos Souris
152 100+
Well, clearly there was a difference in us about the language.
I referred to C99.. and newb99 probably to a C++ Standard... jyots was probably referring to C++ too..
Since i am not familiar with C++ standards i assumed that (since it is told C++ has compatibility with C) it would be the same...
Just wrong of us not to clarify the language and look at both languages as we should do...
Jan 11 '09 #17
JosAH
11,448 Expert 8TB
@Tassos Souris
Indeed; being a C person myself and *knowing* that none of the ++ nor -- operators yield an lvalue in any of the C versions (ranging from K&R1 up to the current version) I bluntly assumed that the OP was talking about C.

I heartly support splitting this forum in a C and C++ forum just to get rid of the confusion (we even get C# questions here because that also starts with a C ;-)

Assumptions are wrong of course but I fail to see what the benefit would be for the pre ++ operator yielding an lvalue:

Expand|Select|Wrap|Line Numbers
  1. int y= 42, x= 54;
  2.  
  3. ++y= x;
  4.  
Great, what's the benefit of ++y being an lvalue? It ends up with the value 54 anyway, effectively annihilating the pre increment ...

kind regards,

Jos

edit: I failed to find a satisfactory explanation in Bjarne Stroustrups brick of a book ...
Jan 11 '09 #18
newb16
687 512MB
@JosAH
you can pass it by reference
Expand|Select|Wrap|Line Numbers
  1.  void bar(int &x);
  2. ...
  3. bar(++x); // is it different than x++;bar(x); ?
  4. //fail: bar(x++);
  5.  
Jan 12 '09 #19
Banfa
9,065 Expert Mod 8TB
This

y=(++x)++; //OK

may be valid syntax and compilable in C++ but if x is a basic type, say an int, it still falls fowl of modifying a variable twice between sequence points and thus produces undefined behaviour.

On the other hand if x is an object with an overloaded operator++ then rather than being an actual operator it is a function call and it is not undefined behaviour, as there is a sequence point "At a function return, after the return value is copied into the calling context."

Needless to say this sort of silly coding structure is best avoided.
Jan 12 '09 #20
Tassos Souris
152 100+
isn't C++ fully backwards compatible with C (as much as i know.. i am not a fun of C++)?
How can there exist such a difference?
Jan 12 '09 #21
JosAH
11,448 Expert 8TB
@Tassos Souris
Nope; this is one of the peculiarities; there are more: quite a few of them handle widening conversions and the scope of variables. C and C++ are two different languages after all (although people may not realize this).

kind regards,

Jos
Jan 12 '09 #22
Banfa
9,065 Expert Mod 8TB
No C and C++ are more like siblings than C++ a descendant of C. They have a large area of functionality that they both support but each language has things that are not part of the other Compatibility of C and C++ - Wikipedia, the free encyclopedia

Perhaps the easiest example of this is
int *pi = malloc(sizeof *pi);
which is completely valid C and in fact in C it would be best practice not to cast the output of malloc. This wont compile in C++ which doesn't allow assignment of non-equal pointers, it would require a cast.

The thing is people almost never compile C code as C++, most C++ compilers include a C compiler and when compiling a file with a .C extension compile it as C.
Jan 12 '09 #23
JosAH
11,448 Expert 8TB
@Banfa
I didn't say that; all I said was that C and C++ are two different languages.

kind regards,

Jos
Jan 12 '09 #24
Banfa
9,065 Expert Mod 8TB
@JosAH
I didn't say you did, that's my opinion, look at the post times.
Jan 12 '09 #25
JosAH
11,448 Expert 8TB
@Banfa
Well, you *could've* typed that all in two minutes ;-)

kind regards,

Jos (<--- slow typer of the year)
Jan 12 '09 #26

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a...
15
by: Robert Swan | last post by:
I'd like to know why the following program outputs 1, and not 0. #include <iostream> class a { int v; public: a():v(0){} a& operator++(int) { v++;
13
by: kailasam | last post by:
Hello, Iam having a doubt. Which is more efficient post increment or Pre increment? I have read that preincrement is efficient than Post increment. Iam not able to think how it is? For an...
18
by: Patrick Wood | last post by:
I found a problem with C# and post increments. I was going through some source code in c++ and found someone did a post increment: int x=0; for(int i=0; i<10; i++) { x = x++; }
8
by: Angel Tsankov | last post by:
Should pre/post increment/decrement return const or non-const? What about other functions?
1
by: mohsin | last post by:
hi everyone well i m little bit confused about the use of pre an post increment infact my thinking contradict with the logic of programe lets consider an example x=5; y=x++; z=x; after the...
11
by: divya_rathore_ | last post by:
The code: int aaa = 100; printf("%d %d %d\n", --aaa, aaa, aaa--); printf("%d\n", aaa); prints: 99 100 100 98
13
by: jehugaleahsa | last post by:
Hello: In C++, you had to distinguish between post and pre increments when overloading. Could someone give me a short demonstration of how to write these? I get the impression that are...
5
by: simudream | last post by:
//hi maybe helpful others can look at this code and //tell me why the class code won't behave like //intrinsic types with post and pre increment //Version: 1.00 #include <iostream> using...
3
by: Stang1 | last post by:
The following statement: line_buf = ' '; is equivalent to: line_buf = ' '; line_len++;
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.