Increment error

Increment error please help!!!
1
2
3
4
5
6
7
8
9
10
#include<iostream>

using namespace std;

main()
{
int a=7;
cout<<a++<<a++;
cout<<a;
}


The output of the program must be like this: 789.
But, output I am hiving is: 879. What's this?? Please help.
Thank you
Modifying the same variable inside the same statement like that is not reliable. Split it into separate statements (lines) and it should give you the result you expected.

For the future you might want to turn up the compiler's warning level so that it can warn you about problems like this. If you're using GCC (or MinGW on Windows) you should at least use the -Wall compiler flag.

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
Last edited on
Thank You friend but when i compiled this same program in my android phone (Application: C4droid (GCC compiler for android)) then it worked successfully.
Why is this happening?
Peter87 said :
Modifying the same variable inside the same statement like that is not reliable. Split it into separate statements (lines) and it should give you the result you expected.


I think the code might act differently if compiled by different compilers. It is just better if you write it in separate statements.

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>

using namespace std;

int main() // Does the code compile?
{
  int a=7;
  cout << a++; 
  cout << a++;
  cout << a;
}
Last edited on
@suksham

I have compiled statements like these on C4Droid too. I am surprised I finally met someone on here who uses it too.

See, modifying the same variable and using it in the same statement is undefined. This means you will get different results on different compilers/machines.
Thank you Guys its really Helpful ... And I often use C4droid to code while outside my home! :)
@suksham

By the way, do you used the paid version or free version of C4Droid?
Topic archived. No new replies allowed.