0

Recently I've come across this code, Which I assumed to understand and didn't bother to run, My answer (that i assumed in my head) was confirmed by the book Let Us C. Later when I try to give it a run in C, to my surprise the output was completely contrasting. C++ is giving the output which I expected.

C++ :

int a=1;
cout<<a<<a++<<++a;

Output :

113

C :

int a=1;
printf("\n%d %d %d",a,a++,++a); 

Output:

3 2 3

I expected this to be the same as the output produced by C++.

I don't have a very clear understanding of Precedence and Associativity of Operator. But this output seems to be out of blue.

I'm using Atom as an editor and C / C++ compiler GCC.

0_0perplexed
  • 85
  • 11
  • KamilCuk But my question is Why these two programming languages are behaving differently? – 0_0perplexed Jun 13 '20 at 07:30
  • 4
    `two programming languages` C __is not__ C++. These are _separate_ programming languages. `are behaving` Those constructs are undefined behavior. Both compilers should spawn demons. (and in late C++, it's valid, because the behavior was changed from undefined to unspecified). – KamilCuk Jun 13 '20 at 07:32
  • 2
    In C, this is just undefined behavior. In C++, this may be legal. So you should remove all the stuff about C, and just ask if this is legal in C++. As for why they behave differently, it's because they are different languages with different rules. – user3386109 Jun 13 '20 at 07:32
  • 1
    It's undefined behaviour. That means in each case the result is unpredictable so it's a moot point to try and explain why they are different. – kaylum Jun 13 '20 at 07:33
  • And the fact that C++ is superset of C should be ignored? – 0_0perplexed Jun 13 '20 at 07:33
  • 2
    Yes it should be ignored. They are different languages and that is how you should treat them. – kaylum Jun 13 '20 at 07:34
  • It's not strictly a superset of C. It's a C derived language, but it's not C. Writing legal code that compiles and runs correctly in both languages is not trivial. What you are doing, exploits undefined behavior. – jwdonahue Jun 13 '20 at 07:36
  • 2
    "how do I know when it is undefined and when It's not?". Read the language specs. Search for C standard and C++ standard, chose the appropriate year(s) for your compilers and read them thoroughly. – jwdonahue Jun 13 '20 at 07:38
  • This is not an issue between C and C++. A short example shows this is in 1 compilation unit in C++: https://godbolt.org/z/HTLM2Y – Robert Andrzejuk Jun 13 '20 at 07:44
  • So that an expression is evaluated in a predictable manner, in C++17 the order-of-evaluation rules have been improved: https://en.cppreference.com/w/cpp/language/eval_order – Robert Andrzejuk Jun 13 '20 at 07:46
  • Re "*And the fact that C++ is superset of C should be ignored?*", C++ isn't a superset of C. – ikegami Jun 13 '20 at 08:33
  • C++ is not a superset of C. C has variable-length arrays. C++ does not. That's not the only feature that C has and C++ doesn't. Here is an incomplete list with C features that are not in C++ https://stackoverflow.com/questions/3879636/what-can-be-done-in-c-but-not-c Some of them will be implemented in C++20 – Thomas Sablik Jun 13 '20 at 11:34

0 Answers0