this expression has undefined o/p but how could it give 4 4 pls suggest any possible mechanism by which 4 4 is produced
15 12623 Banfa 9,065
Expert Mod 8TB
It's undefined behaviour, anything can happen.
However with the post and pre increment and decrement operators no guarantee is provided about when the increment or decrement is done. Only about what value will be used for the expression of the operator.
i.e. ++i guarantees that the value will be the value produced by adding 1 to i but does not guarantee when exactly i will be assigned this value.
In your expression the compiler could have quite simply evaluated both ++ operators before putting anything on the stack for the function call. But as you an I have said this operation is undefined speculation is pointless.
as per my view..the o/p should be 3 and 4...why u getting 4 4
Read Banfa's post again.
When you change the value of the same variable more than once in a statement, the results are indeterminate.
Seeing a display of 3 and 4 requires an assumption. Maybe there's a compiler that agrees with you, and then, maybe not.
hi,
the output is 3 2.i don't know how you are supposed to get 4 4 .
regards,
santosh
Please read posts from Banfa and weaknessforcats. The behavior varies from compiler to compiler.
Regards
Dheeraj Joshi
Banfa 9,065
Expert Mod 8TB Vaibhav Joshi: as per my view..the o/p should be 3 and 4...why u getting 4 4
santechselva: the output is 3 2.i don't know how you are supposed to get 4 4 .
OK I have said this already but I will say it again.
The output is undefined behaviour, that is the compiler is free to do anything it wants and is free to create a program that does anything including but not limited to - Program halts with no error
- Program appears to operate as expected
- Program formats hard disk
- Program sends all your personal data to the CIA
- Demons fly out of your nose
Relying on any particular output of code like this is at best foolhardy and at worst going to result in a very non-working program.
Trying to guess what the output will be is pointless, avoid writing code that exhibits this behaviour.
Program sends all your personal data to the CIA
Demons fly out of your nose
Really? lol.
Regards
Dheeraj Joshi
Banfa 9,065
Expert Mod 8TB
Really? lol.
Well the standard does not prohibit it :D C++ Standard
1.3.12 undefined behavior [defns.undefined]
behavior, such as might arise upon use of an erroneous program construct or erroneous data, for which this
International Standard imposes no requirements. Undefined behavior may also be expected when this
International Standard omits the description of any explicit definition of behavior.
Ha ha ha. I am laughing out loud.
Regards
Dheeraj Joshi
Here is a warning I gave awhile back concerning invoking undefined behavior. It mirrors what Banfa said above.
You may think you have figured out how your particular compiler handles a particular kind of undefined behavior ... but you're wrong. Even if your program does the same thing a thousand times in a row, there is no assurance that it will behave the same way the next time you run it. And there is certainly no assurance it will behave the same way after you upgrade to the next version of the same compiler from the same vendor.
Here the answer will b 4 4 only,
Actually the syntax will be evaluated from right to left and the values are stored in i.
1) ++i = 2+1 =3
2) ++i = 3+1 =4
But while printing the left value of i will be assigned to ++i values.
Have a look here :
int i=2;
printf("%d %d %d %d %d", i++,++i,++i,i++,i++);
output: 6 7 7 3 2
@raviroshan But while printing the left value of i will be assigned to ++i values.
This is an assumption on your part.
If the calling sequence is Pascal, arguments are evaluated left to right, but if it's _cdecl arguments evaluated right to left.
Try to not build restrictions into your code like this.
Also, please not that the order of function argument evauation is not the same as changing a variable more than once in the same sequence point.
Plus when you code (i++,++i,++i,i++,i++); the variables will get incremented but the compiler has until the ; to do it. Different compilers will give you different results.
Banfa 9,065
Expert Mod 8TB
raviroshan you are trying to apply logic to an undefined construct and that is just never going to work out well.
On my Windows XP box if I compile your code with MinGW32 (Windows port of gcc) then I get the output 6 7 7 3 2 as you suggested.
On the other hand if on the same computer I use the compiler from VC++ 10 then I get the output 4 4 4 2 2.
As I have already stated, ignoring the order of evaluation of parameters to a function which is platform defined, this code breaks the rules on modifying the value of an object (i in this case) more than once between sequence points. The result of breaking that rule is undefined behaviour and once undefined behaviour has been invoked you can say nothing sensible about what the output might be. Avoid undefined behaviour.
hello,raviroshan sir can you explain me little bit clearer how 67732 was printed?
thank u sir.
@suesh:
This is undefined behavior. Any time you change the value of a variable in a single statement you have no idea what different compilers will do with the same code.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: |
last post by:
When we have a structure in the following form
typedef struct {
int I1;
int I2;
int I3;
int I4;
float F1;
float F2;
float F3;
|
by: der |
last post by:
Hello all,
I've a question about order of evaluations in expressions that have && and
|| operators in them.
The question is: will the evalution go left-to-right, no matter what -- even
if the...
|
by: Shill |
last post by:
I have several questions.
In C, AFAIU, a for loop is just syntactic sugar for a while loop.
for (i1; i2; i3) i4;
is equivalent to
i1
while (i2) {
|
by: Shawn Chisholm |
last post by:
Hi, I am trying to deal with a deadlock situation caused by foreign key references on insert and I was wondering if anyone knows what order the foreign keys are locked (or evaluated) in for a...
|
by: dragoncoder |
last post by:
Consider the following code.
#include <stdio.h>
int main()
{
int i =1;
printf("%d ,%d ,%d\n",i,++i,i++);
return 0;
}
|
by: berns |
last post by:
Hi All,
A coworker and I have been debating the 'correct' expectation of
evaluation for the phrase a = b = c. Two different versions of GCC
ended up compiling this as b = c; a = b and the other...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| | |