C programming unary operator precedence [duplicate] - c++

This question already has an answer here:
Difference between a+++++b and a++ + ++b [duplicate]
(1 answer)
Closed 7 years ago.
#include <stdio.h>
int main()
{
int c=10,b;
b=++c+++c;
printf("%d",b);
return 0;
}
Could someone please let me know,why it is throwing compilation error?

The gibberish is tokenised as
++ c ++ + c
and parsed as
((++c)++) + c
This tries to increment the rvalue yielded by ++c, which isn't allowed. You can only increment an lvalue (or a class type, in C++).
Even if it were allowed, this would give undefined behaviour: you'd have an unsequenced modification and use of the value of c.

The compilation error would be solved if you put a space in ++c + ++c
b = ++c + ++c;
But that just fixes the compilation error. What you are doing is Undefined Behavior. Trying to change the value of c more than once in the same expression using ++ will lead to undefined Behavior.
Read Why are these constructs (using ++) undefined behavior?

Related

C++ "value++" causes warning but "value+1" doesn't? [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 3 years ago.
The code:
int a = 0;
a = ++a % 5;
causes the warning:
warning: operation on 'a' may be undefined [-Wsequence-point]
a = ++a % 5;
~~^~~~~~~~~
with various compilers such as gcc when compiling with -Wall
Yet this code, works fine?
int a = 0;
a = (a + 1) % 5;
Why is this a warning, and can it safely be ignored?
Wrapping it in brackets etc. doesn't seem to make the warning go away.
Edit: For clarification, I was using C++17 compiler when seeing these warning messages.
a = ++a % 5;
a is modified two times. Before C++11, this is undefined behavior — it is not specified that the increment is committed before the assignment. Since C++11, the side effect of the pre-increment on the RHS is guaranteed to be evaluated first, and a is guaranteed to be 1.
a = (a + 1) % 5;
Here, a is only modified one time. The resulted a is guaranteed to be 1.
Per comment: operator precedence does not determine the order of evaluation. Although assignment has higher precedence, the order of evaluation is still unspecified (prior to C++11).
EDIT:
Before C++11: Because with ++a you're modifying a while you're modifying a through assignment, therefore undefined behavior.
After C++11: See #L.F.'s answer.

Ternary operator in C vs C++ [duplicate]

This question already has answers here:
Errors using ternary operator in c
(5 answers)
Closed 8 years ago.
There are a lot of differences between C and C++ and came to stuck on one of them
The same code gives an error in C while just executes fine in C++
Please explain the reason
int main(void)
{
int a=10,b;
a>=5?b=100:b=200;
}
The above code gives an error in C stating lvalue required while the same code compiles fine in C++
Have a look at the operator precedence.
Without an explicit () your code behaves like
( a >= 5 ? b = 100 : b ) = 200;
The result of a ?: expression is not a modifiable lvalue [#] and hence we cannot assign any values to it.
Also, worthy to mention, as per the c syntax rule,
assignment is never allowed to appear on the right hand side of a conditional operator
Relared Reference : C precedence table.
OTOH, In case of c++, well,
the conditional operator has the same precedence as assignment.
and are grouped right-to-left, essentially making your code behave like
a >= 5 ? (b = 100) : ( b = 200 );
So, your code works fine in case of c++
[ # ] -- As per chapter 6.5.15, footnote (12), C99 standard,
A conditional expression does not yield an lvalue.
Because C and C++ aren't the same language, and you are ignoring the assignment implied by the ternary. I think you wanted
b = a>=5?100:200;
which should work in both C and C++.
In C you can fix it with placing the expression within Parentheses so that while evaluating the assignment becomes valid.
int main(void)
{
int a=10,b;
a>=5?(b=100):(b=200);
}
The error is because you don't care about the operator precedence and order of evaluation.

Expression evaluation in C++ involving unary operators [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
Why does not C/C++ evaluates expression in order of left to right in these cases:
Initially x=1
Evaluating x + ++x gives 4.
If normal evaluation is carried out (precedence of ++ is higher than +) then the result should be 1 + 2 = 3
Similarly:
x + ++x + x gives 6
x + x + ++x gives 4
Why are results different?
More Cases:
x + x++ +x gives 5
What rule is followed by C/C++ instead?
Specifically the results of these expressions are not defined, this is because of Cs requirement for multiple accesses (excluding cases where all accesses are reads) to always have a sequence point between them (such as a ; or ,). The results you are getting there are effectively random and would depend on your compiler or theoretically could even change between runs of your program, please see here for information on sequence points:
http://en.wikipedia.org/wiki/Sequence_point
And here if you want to learn about undefined behavior (what your misuse of the variable causes):
http://en.wikipedia.org/wiki/Undefined_behavior#Examples_in_C_and_C.2B.2B

Operator precedence and operator associativity rules in c++ [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 9 years ago.
I do not understand why the output of following program is 63:
#include <iostream>
int main() {
int a = 20;
a += a + ++a;
std::cout << a;
}
I was expecting it to be 61. What exactly a += a + ++a; does?
Standard says: "Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression" (5 Expressions, §4), i.e. the following:
a += a + ++a
yields undefined behavior just like:
a = ++a;
does already. It also says: "the prior value shall be accessed only to determine the value to be stored", i.e. if you want to change a, you can use a in the same expression just to retrieve the previous value:
a = a + 1; // OK
... "otherwise the behavior is undefined."
You're triggering undefined behavior and there is no 'correct' answer. Your compiler can chose what order to evaluate the arguments of the plus operator.
it looks like ++a is evaluating before the rest of the expression, so it's as though a is 21` in a statement like
a += a + a;
at any rate, don't use ++a inside of an arithmetic expression like that anyway. It's confusing for people, and is probably undefined behavior

`y=++y`, is this standard compliant? [which appears in a test by Microsoft] [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
So why is i = ++i + 1 well-defined in C++11?
(3 answers)
Closed 9 years ago.
I know this looks familiar but it is brought to me as a problem in a test by Microsoft to recruit interns. It seems to me that y=++y is not standard compliant, but I think maybe it would be better to be sure (not sure that I'm better than those who write up these tests at MS). So I'm asking your advice. Do you think expressions like this is standard-compliant and does not result in undefined behaviour?
#include <stdio.h>
int main(){
int a = 10;
int b = 10;
a = ++a; //What ???
b = b++; //What ???
printf("%d %d\n",a,b);
return 0;
}
gcc complains about it when used to compile with -Wsequence-point. (It is not explicitly stated whether it is a C or C++ specific problem.)
But only four answers provided:
a) 10 10
b) 11 10
c) 10 11
d) 11 11
Although one is not restricted to select only one answer ( so maybe I should choose all four? )
Well, in my opinion, between self-incrementing and assignment there is no sequence point. So this violates the specification. Doesn't it?
They're both well-defined behaviour as per C++11. C++11 doesn't even have sequence points, so a sequence-point related warning is obviously outdated. The assignment operator imposes sequencing on its arguments.
Edit:
Whilst everyone can agree that i = ++i is now well-defined behaviour, it is rather non-trivial to decide the definedness of i = i++. Intuitively, I should describe it as well-defined, but the Standard clearly states that
i = i++ + 1;
is UB, and I'm not seeing the + 1 making any difference here.
The short is, if you wanted to answer this question, you would need to be an expert on the decidedly non-trivial C++11 sequencing rules, which it appears that I am not, but it appears that the answer is "None of the above because UB".
As per C++03 standard,
Both of the below statements result in undefined behavior:
a = ++a;
b = b++;
As you correctly stated, g++ points out below warning:
warning: operation on ‘a’ may be undefined [-Wsequence-point]
Note that in case of UB, the answer can be anything apart from (a), (b), (c), (d).
Ideally there should be an option for "undefined behavior" in the test question. But often, the person who prepares the question would simply compile in XYZ compiler and submit it.
Here is a related SO post explaining Undefined Behavior and Sequence Points.
a = ++a;
b= b++;
These operations are considered as undefined.
Each compiler can do as its wish. output varies on compiler the examiner framed the question.
I think it's D) since ++a means increment a and then execute the operation, so it will be like doing a=11. b++ means do the operation and then increment, so you'll be assigning b=10 but then b is incremented giving it the value b=11.
Well for the purists: It doesn't "mean anything and then anything", but that's it's behavior. I tested it (using visual c++ and g++) and my answer is correct a=11 and b=11.