Reading from left to right or right to left?

I've been doing questions that require us to give the output and up until now I thought my concepts were clear until I came to these specific questions. I have usually read statements with pre and post increment operators from left to right and I've never faced a problem...until...

Normally, code like this:

1
2
m = 25;
cout<<++m+m;


Should according to me, reading from left to right, give an output 25+26 i.e. 51.
But it gives 52!
Even reading from right to left does not give that!


Then, I have also come across code like this:

1
2
int i = 20;
cout<<i<<i++<<++i;


Which initially, I had read from left to right to give an output of (considering spaces between for clarity) 20 20 22 BUT actually gives an output of 21 21 22 which basically to me meant that we had read it from right to left.


I really need someone to shed light on what seems to me pretty confusing!
Do not worry, those are undefined behaviour. There is no "this before that" rule in the language for equations like in your examples and thus each compiler can choose how they "read".

In other words, write statements that avoid such dilemmas.
@neha candy

When one writes correct code, there is operator precedence with associativity, and these read both left to right and vice versa .

http://en.cppreference.com/w/cpp/language/operator_precedence

One needs to pay attention to these, sometimes one might need to use parentheses to get the intended evaluation order.

The following may be too advanced right now (I am guessing).

My main concept is that some declarations are more easy to understand when from read from right to left, notably pointers with const:

1
2
3
4
5
int i = 0;
int* PtrInt = &i; // pointer to int, the pointer can be made to point to some thing else, and the int value can change too
int* const ConstPtrInt  = PtrInt; // constant pointer to int, the pointer variable can't be changed to point at something else, but the int value can be changed
const int* const ConstPtrConstInt  = PtrInt; // constant pointer to const int, the pointer variable can't be changed to point at something else, and the int value can't be changed either, doesn't quite read right to left but you probably get the idea :+)
 int const* const AlternateFormConstPtrConstInt  = PtrInt; // constant pointer to const int, the pointer variable can't be changed to point at something else, and the int value can't be changed either, it does read right to left 


Note that I put the * right next to the type, it's a convention I like because C++ is all about types. Other equivalent forms are :

1
2
int *Ptr;
int * Ptr;


But I strongly prefer int* Ptr;

The exact same thing happens with references:

void MyFunctionDeclaration (std::string& StrRef);
Last edited on
Assuming that m and i are scalars (like int),

This is undefined behaviour: cout << ++m + m ; (unsequenced access and modification of a scalar)

This is well defined in C++17 (should print out 20 20 22 as you expected it would):
1
2
int i = 20;
cout << i << ' ' << i++ << ' ' << ++i;

This was added in C++17:
In a shift operator expression E1<<E2 and E1>>E2, every value computation and side-effect of E1 is sequenced before every value computation and side effect of E2 http://en.cppreference.com/w/cpp/language/eval_order

Prior to C++17, the evaluations were unsequenced and the behaviour was undefined.
Last edited on
Topic archived. No new replies allowed.