I am a bit in confusion on when I compile my program, it's reading from Right to Left or Left to Right. I have a sample program and a particular program from the book. I heard that when data is processed, it reads from Right to Left, however the compiler usually reads from Left to Right. I am particularly confused on this.
1. I would think this reads from Right to Left, taking the data of "7" which is equal to "B" then "B" is equal to "A". Or is it from Left to Right where "A" is equal to "B" which is equal to "7"? Thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
usingnamespace std;
int main()
{
int A, B, C;
A = B = 7;
cout << "First: " << A << " Second: " << B << endl;
system("pause");
}
1. Your program won't even compile without including the header file where system() is defined.
2. Please avoid system() calls for preventing the window to close. There are better, safer methods like cin.get(). The call "pause" isn't portable either.
3. First, the integer values A, B and C are declared. In words, you say, the variables A and B have the value 7. I believe the compiler reads from right to left here because A=B cannot be executed (B has no value). Then, B is set to 7 and A to B (=7).
The '=' in your code is assignment operator.
Assignment takes the value of its right side and stores it into variable on its left side.
B = 7 -- value (7) is assigned into variable (B). The B changes. Its value is 7 after assignment.
You have a chain of two assignments. They are either: (A = B) = 7; // #1
or A = (B = 7); // #2
What does the operator '=' return?
A reference to its left side.
In case #1 we first assign the value of B into A. The A changes. Then we assign value 7 to A (that was returned by the first assignment). The A changes again. The B did not change. Its value remain unknown. The A has value 7.
In case #2 we first assign the value 7 into B. The B changes. Then we assign value of B, which was returned by the first assignment, into A. The A changes. Both A and B have now value 7.
Operators that have the same precedence are bound to their arguments in the direction of their associativity.
For example, the expression a = b = c is parsed as a = (b = c) ... because of right-to-left associativity of assignment, but a + b - c is parsed (a + b) - c ... because of left-to-right associativity of addition and subtraction.
Precedence and associativity are compile-time concepts and are independent from order of evaluation, which is a runtime concept.
I decided to take one example in regards to the Right-to-Left and Left-to-Right examples and test it out.
1. 1st is Left-to-Right according to the cppreference. Because it's Left-to-Right, then I read it as "a" which is "5", we add "1" to "a" because of the "++". So the next time we use "a" will be "6". Can't you read it from Right to left also? Like you have "++" which is adding "1" to "a", so the next time you use "a" will be 6?
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
usingnamespace std;
int main()
{
int a = 5;
cout << "New data: " << a++ << endl;
cout << "New data: " << a << endl;
cin.get();
}
----------
2. My second is the opposite with the Right to Left rule. In this example according to the reference website, you have "a" which is "5", and on the left of "a" you have "++" which is adding "1" to "a", so "a" is automatically now "6". So when you cout "++a" it's "6" and using "a" after that is also "6". Can't you read it from Left-to-Right also? Can it be read as "++" means adding "1" to "a" so it means that when couting "++a" automatically changes to "6" and using "a" after that will also be "6"?
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
usingnamespace std;
int main()
{
int a = 5;
cout << "New data: " << ++a << endl;
cout << "New data: " << a << endl;
cin.get();
}
The ++ in ++a and a++ are two different operators.
In ++a, the operator is the prefix increment operator
In a++, the operator is the postfix increment operator (aka suffix increment operator)
Precedence and associativity are used to determine what the operands are; they do not change the meaning of the operator itself.
In the expression *p++, the postfix increment operator has a higher precedence than the indirection operator (aka dereference operator). So, this is parsed as *(p++)
In the expression *++p, the prefix increment operator and the indirection operator have the same precedence. These associate from right to left. So, this is parsed as *(++p) (the ++ in ++p is still the prefix increment operator).