#include<iostream.h>
void main()
{
int i = 10;
clrscr();
cout << i << i++ << ++i;
getch();
}
The output is
11 12 12
how these output is possible? plz give details explain
6 9235
The provided code will not produce the output you have given. The correct output should be:
(1) 10,original value, followed by,
(2) 10, the second value. Here, the ++ is executed after the insertion operator.
Now at the beginning of the third increment you will have 11.
(3) At this time, the ++ is executed before insertion operator works. so you will get 12.
In short the output should be 10 10 12.
BTW, what is the point of having getch here???
Banfa 9,065
Expert Mod 8TB
Well I actually get 12 11 12
vmohanaraj I am afraid you are wrong in your explaination
The line of code
cout << i << i++ << ++i;
Invokes undefined behaviour, that is calling this line of code allows the compiler to do anything it wants to. This is because you have altered a single variable (i) more than once between sequence points.
There is no point trying to explain the output, it is very possible that it will be different on every platform that you try it on.
Oops. It is really confusing. I really got 10 10 12, the way I thought it should be. So i went on explaining in that way. I am sorry for that.
However, what is the reason for the undefined behaviour? The statements are executed from left to right. I think that is true for << operator as well. That is why we get the output in that order. Therefore, is not it reasonable to expect that all of us should have got atleast 10 as the first number?
Banfa " Well I actually get 12 11 12" can you explain how this output is evaluated?
i got 12 11 11. i used a printf statement.
printf(" %d %d %d" ,i,i++,++i);
can you explain how the printf will be evaluated?
I Thnk u guys are wrong
In c++, it works from right to left associativity.....thts why we get the answer as 121111
But in C, not in the way
The standard does not mandate any specific behaviour when you modify a
value twice between two sequence-points. Since no specific behaviour is
mandated, the behaviour you see is okay. Note however that that has
nothing to do with the increment-operator in particular.
Sign in to post your reply or Sign up for a free account.
Similar topics
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: lovecreatesbeauty |
last post by:
Hello experts,
Why can this difference between prefix increment/decrement and
postfix increment/decrement reside in built-in operators for built-in
data types? Thanks.
// test.cpp
//
//...
|
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: Anjali M |
last post by:
#include <stdio.h>
int main()
{
int number = 10;
printf("Number is: %d\n", number);
printf("Sizeof of number is: %d\n", sizeof(number++));
printf("Number now is: %d\n", number);
|
by: Angel Tsankov |
last post by:
Should pre/post increment/decrement return const or non-const?
What about other functions?
|
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: SPOILED36 |
last post by:
I am building a database to track attendance. I have one main form with multiple subforms. Within one of the subforms name sfrDailyAttendance, I also have cascading combo boxes (cboCategory and...
|
by: mohammad ismail aakhil |
last post by:
i have overladed unary ++ both pre and postfix increment operator using member function. both the operator function differ by arguements.v use a additional argunent of type int.
how does complier...
|
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: 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:
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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:
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...
|
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: 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...
|
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...
|
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,...
| |