what is difference between
1. y=++x++;
2. y=(++x)++;
3. y=++(x++);
25 4381 @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
y=(++x)++;
this statement is givin no problem... but rest of them are givin error..
@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
Same here on bcc32, got any suggestions on another (freeware) compiler to use?
Download codeblocks it is free and update IDE and also come with mingw compiler
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.
This code: -
y=++x++; //ERROR
-
y=(++x)++; //OK
-
y=++(x++); //ERROR
-
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:
However, y= y=(++x)++ is OK because the result of ++x is an l_value:
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.
@weaknessforcats
No this is not correct: - 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.
@Tassos Souris
It compiles without a problem though.
But if you try: - int x = 0;
-
int y = 0;
-
++x = x;
-
++y = y+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?
Neither of them is correct. As Jos said the result is not a lvalue.
Visual Studio gives (correctly) a compile error: -
error C2106: '=' : left operand must be l-value
-
For example, consider this: -
int x[ 2 ];
-
int *ptr = x;
-
This is not correct:
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
Because the 'result' of the assignment "does not depend" on the ++ operator.
Let's examine the first case:
... the ++ does not produce a lvalue but then the * operator takes effect so everything is ok
... 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.
@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 -
printf("%p\n", &a );
-
printf("%p\n", &++a );
And it should print the same.
@newb16 correct result:
++ 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: -
int y = 0;
-
size_t size = sizeof( ++y );
-
, and also, since the operand of the sizeof() operator is not evaluated if it is not a variable length array type, y remains 0.
@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.
@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.
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
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...
@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: -
int y= 42, x= 54;
-
-
++y= x;
-
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 ...
@JosAH
you can pass it by reference - void bar(int &x);
-
...
-
bar(++x); // is it different than x++;bar(x); ?
-
//fail: bar(x++);
-
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.
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?
@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
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.
@Banfa
I didn't say that; all I said was that C and C++ are two different languages.
kind regards,
Jos
Banfa 9,065
Expert Mod 8TB @JosAH
I didn't say you did, that's my opinion, look at the post times.
@Banfa
Well, you *could've* typed that all in two minutes ;-)
kind regards,
Jos (<--- slow typer of the year)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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++;
|
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...
|
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++;
}
|
by: Angel Tsankov |
last post by:
Should pre/post increment/decrement return const or non-const?
What about other functions?
|
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...
|
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
|
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...
|
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...
|
by: Stang1 |
last post by:
The following statement:
line_buf = ' ';
is equivalent to:
line_buf = ' ';
line_len++;
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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...
| |