#include<iostream>
int main()
{
int a = 5;
a = (a = 10, a++, a--);
std::cout << a;
}
Output is 11,
But when I modify the line
a = a=10,a++,a--;
Output is 10
What effect does removing the ( ) operator has and in what order the operators are being executed.
The + and - operators take precedence before the assignment operator and the +,- signs are read by the compiler from left to right, whereas the assignment operators are read by the compiler from right to left. So:
1) a++
2) a--
3) a=10
4) a =
The c++ compiler will pass your complete set code to a binary tree which will rearrange your code according to the precedence of the operator = + - etc in left node or right node
Related
The output should be false, as false&&true is false. However, the compiler is giving 1 as output.
#include<iostream>
using namespace std;
int main()
{
int a = 10;
int b = 13;
bool flag = true;
cout << ((b - 4) < a) && !flag;
return 0;
}
This is because << has higher precedence than &&, your expression should be:
cout << (((b-4)<a) && !flag);
A more detailed explanation:
One of the first things that any C++ compiler does when processing your source code is to construct an abstract syntax tree (AST) representing your source code. The AST is a tree structure that encodes -among other things- the precedence of different unary and binary operators. An AST for the expression you have written (cout << ((b - 4) < a) && !flag;) would look something like this:
&&
/ \
<< !
/ \ \
cout < flag
/ \
- a
/ \
b 4
How do we know that e.g. && must be the root of the AST or that sub-expressions in parentheses are evaluated first? Because the C++ operator precedence rules say so. These are similar to the rules we use when doing math on paper (e.g. do multiplication before addition) but extended to all operators supported by the language.
We can use the AST to see in what order the sub-expressions of this expression are evaluated, the general rule being that evaluation happens in "bottom up" order, i.e. from the leaves to the root of the AST. In this particular case, evaluation could (conceptually) happen like this:
// left subtree
b - 4 = 9
9 < a = true
cout << true = cout (operator<< returns a reference to cout)
// right subtree
!flag = false
And finally we're left with cout && false. cout implements operator bool which allows it to be implicitly cast to bool, yielding true if the stream has no errors. Because that is the case here, we finally have true && false == false the value of which is unused.
So you can see that your program outputs 1 because during evaluation of the left subtree, cout << true is executed. Afterwards, cout && !flag, though well formed, essentially does nothing because it has no side effects and you don't use the result.
I have a sample midterm question that I am not too sure about. Here it is:
#include <iostream.h>
void f( int i )
{
if( i = 4 || i = 5 ) return;
cout << "hello world\n" ;
}
int main()
{
f( 3 );
f( 4 );
f( 5 );
return 0;
}
So I understand that the logical OR operator has a higher precedence and that it is read left to right. I also understand that what's being used is an assignment operator instead of the relational operator. I just dont get how to make sense of it all. The first thing the compiler would check would be 4 || i? How is that evaluated and what happens after that?
Let's add all the implied parentheses (remembering that || has higher precedence than = and that = is right-associative):
i = ((4 || i) = 5)
So, it first evaluates 4 || i, which evaluates to true (actually, it even ignores i, since 4 is true and || short-circuits). It then tries to assign 5 to this, which errors out.
As written, the code doesn't compile, since operator precedence means it's i = ((4 || i) = 5) or something, and you can't assign to a temporary value like (4 || i).
If the operations are supposed to be assignment = rather than comparison == for some reason, and the assignment expressions are supposed to be the operands of ||, then you'd need parentheses
(i = 4) || (i = 5)
As you say, the result of i=4 is 4 (or, more exactly, an lvalue referring to i, which now has the value 4). That's used in a boolean context, so it's converted to bool by comparing it with zero: zero would become false, and any other value becomes true.
Since the first operand of || is true, the second isn't evaluated, and the overall result is true. So i is left with the value 4, then the function returns. The program won't print anything, whatever values you pass to the function.
It would make rather more sense using comparison operations
i == 4 || i == 5
meaning the function would only print something when the argument is neither 4 nor 5; so it would just print once in your example, for f(3).
Note that <iostream.h> hasn't been a standard header for decades. You're being taught an obsolete version of the language, using some extremely dubious code. You should get yourself a good book and stop wasting time on this course.
The compiler shall isuue an error because expression 4 || i is not a lvalue and may not be assigned.
As for the expression itself then the value of it is always equal to true because 4 is not equal to zero.
I'm studying C++ as a beginner and my book explain me what is assignment but i can't understand a little concept that the book is trying to make me understand:
int a = 3; // a starts out with the value 3
a = a + 7; // a gets the value of a + 7 (that is, 10)
What my book says is: the last assignment deserves notice.First of all it clearly shows that = doesn't mean equals-clearly, a doesn't equal to a + 7.It means assignment, that is, to place a new value in a variable. What does it mean ? how should i read an assignment?
For example:
a = a + 7; // should i read it as: a is equal to a + 7 ?
The code might be expanded in following way:
int a; // declaration of the variable. a has undefined value.
a = 3; // Assign the value 3 to a. a is 3.
a = a + 7; // Assign value of (a + 7) to a. a is 10.
The expression (a + 7) is evaluated first and the value of (3 + 7) is assigned to a.
The token = is used for assignment and doesn't have anything to do with equality.
The assignment means that the right-hand side (rhs) will be evaluated to a single value, then assigned to the left-hand side (lhs)
a = a + 7;
Now, rhs --> a + 7 --> 3 + 7 --> 10, now we have a single value, and will assign 10 to a
In C++ = is the assignment operator. == is the equality operator.
Read the chapter about operator precence (the order in which operators performs operations on its operands).
= (the assignment operator) is a binary operator, has lowest precedence (that is the reason first a+7 operation is performed) and it assigns the result of an expression on its right side to the variable on its left side.
For, a = a + 7, never read it as a equals a + 7, but make habit of reading it as a gets assigned the value of a + 7 (or whatever the expression) is.
The = also replaces the exiting value of variable on left side (a) with the result of expression on right side ( a + 7).
Why is *(ptra + 0).prop not valid and (*(ptra + 0)).prop valid ? . I know that left side of the dot operator must have a structure. But I am still confused. Could someone explain to me the difference between the two ?
class myobj
{
public:
int v;
};
int main()
{
myobj *ptra = new myobj[2]();
*(ptra + 0).v = 12 //Error
(*(ptra + 0)).v = 12 ; //OK
return 0;
}
Because operator . has a higher precedence then operator * , so
*(ptra + 0).v
means
*((ptr + 0).v)
and not
(*(ptra + 0)).v
*((ptr + 0).v) here is incorrect syntax, because (ptr + 0) is not a class or union, so has no any members.
Errors like this one are due to operator precedence. In this case, the first operator that gets executed is operator. and the second that gets executed ins operator*. This is why one line compiles and the other doesn't, even if they look very much alike.
To override this operator precedence, you use parenthesis, as in
(1 + 2) * 3 = 9
as opposed to
1 + 2 * 3 = 7.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ Comma Operator
Uses of C comma operator
I am not new to C++, but this is the first time I see the following code:
int a=0;
int b=(a=2,a+1);
That is C++ code. Can you tell me what is going on here? And how variable b gets value 3?
This code is equivalent to this:
int a = 2 ;
int b = a + 1 ;
First expression to the left of comma gets evaluated, then the one to its right. The result of the right most expression is stored in the variable to the left of = sign.
Look up the comma operator for more details.
http://en.wikipedia.org/wiki/Comma_operator
(a = 2, a + 1); return 3 because in general case operator (a, b) return b, and calculation in (a, b) started from right to left. So, in your case, (a = 2, a + 1) return a + 1, and after operator a = 2 was executed a + 1 return 3.