This question already has answers here:
What does the operation c=a+++b mean?
(9 answers)
Why doesn't a+++++b work?
(9 answers)
Closed 9 years ago.
Given the following code:
int a=0,b=1;
int r=a+++b;
which are the operations performed and in which order?
a++ + b
a + ++b
I this compiler specific or does it depend on the standard?
It is (a++) + b but not because of operator precedence.
It is parsed as (a++) + b because the compiler takes the longest token it can from a sequence of characters. In other words, the lexer keeps reading characters until it encounters something that can't be part of the same token as what it already has.
This is also how it interprets >= as one token instead of > and =, and double as 'double' not 'do uble'.
There are rules of operator precedence for statements like
a || b && c
// "a || (b && c)" NOT "(a || b) && c", because && takes precedence
However, in your case the operators ++ and + have already been determined. Once the operators have been determined, the rules of operator precedence can apply.
There are some good answers to Why doesn't a+++++b work in C? that should explain this in more detail.
That is governed by the operator precedence. Since postfix ++ has a higher precedence than the + operator it binds tighter to a is post incremented then added to b so the result is:
(a++) + b
Related
This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 4 years ago.
a - checks whether input is defined for function b
if (a) {
if (b) {
/* ... */
}
}
clearly works.
Does the &&-operator always check the first input?
if (a && b) {
/* ... */
}
Would this possibly cause undefined behaviour from b function?
Yes, the built-in && short-circuits.
So, if a is false, b will not be evaluated. This is guaranteed, unless there is an overload of operator&&(type of a, type of b).
In the expression a && b, assuming && is the built-in operator and not an overloaded one, b is only evaluated if a evaluates to true.
That's why you can write things like if (a != nullptr && *a).
We call this short circuiting. || also has the property.
As Bathsheba said, if your code is
if (a && b) {
//...
}
Then b will only be checked if a results to true.
This is because the && operator is a so-called shortcut operator.
The same goes for the logical OR (||).
if (a || b)//...
In this case, however, b would only be evaluated if a results to false.
These are not to be confused with the bitwise operators (& and |), which check both values regardless.
This question already has answers here:
What is the "-->" operator in C++?
(29 answers)
Closed 4 years ago.
I have encountered <<++ and >>++ operators many time in`C++, but I don't understand what they are. What is the specific meaning and use of these operators, and how are they different from right shift and left shift operator?
C++ compilers ignore whitespace unless in certain situations such as string literals.
<<++ and >>++ is really just a bit-shift operatior << or >>, followed by an increment operator ++.
Consider this code:
a <<++ b is equivalent to
a<<++b because the spaces are ignored in this context, and then equivalent to
a << ++b (a left shifted by a pre-incremented b)
a << (++b) due to operator precedence. Bit shift operators have lower precedence than incrementation.
There are two separate operators in both cases: left shift (<<), right shift (>>) and increment operator (++).
You can rewrite the following:
a >>++ b
as:
a >> (++b)
This question already has an answer here:
Why doesn't C++ have the ~= and != operators? [closed]
(1 answer)
Closed 5 years ago.
int main()
{
unsigned int a = 0;
unsigned int b = 0;
a ^= b; // ok
a |= b; // ok
a &= b; // ok
a = ~b; // ok
a ~= b; // error : expected ';' after expression
}
^=, |=, and &= are all legal.
Why isn't ~= legal in C++?
Because ~ is an unary operator, not binary.
The short form op= applies to binary operators only, when the first operand is the destination.
~ is only ever a unary operator.
The contraction of a = a # b to a #= b for an arbitrary operator # only makes sense if it takes two arguments; i.e. if # is a binary operator.
Why isn't ~= legal in C++?
That's because C++ does not contain an ~= operator; ~= are just two separate tokens.
As for why C++ wasn't designed that way, you'd have to ask the designer, but I think it's safe to say that it's because C++ was based on C originally and has the same operators as C, with only a few new operators added for the new language features (i.e. :: and ->*).
So you should probably ask this question again for C.
Because the "operator assignments" (like +=, ^=) are based on the original operation having two operands.
For example, a + b has two operands of +, and a += b gives the same net effect as a = a + b.
Operator ~ is a unary operator i.e. it accepts one operand. So a = ~b makes sense, but a = a~b does not. Since a = a~b doesn't make sense, neither does a ~= b.
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.
This question already has answers here:
What does the question mark character ('?') mean in C++?
(8 answers)
Closed 9 years ago.
This may be a bonehead question, but I cannot figure out what the ? exp : other_exp sequence is called.
Example:
int result = (true) ? 1 : 0;
I've tried using the Google machine, but it's hard to Googilize for something without knowing what it's called.
Thanks!
It is called the the conditional operator or alternativly the ternary operator as it a ternary operator (an operator which takes 3 operands (arguments)), and as it's usually the only operator, that does this.
It is also know as the inline if (iif), the ternary if or the question-mark-operator.
It is actualy a rather useful feature, as they are expressions, rather than statements, and can therefore be used, for instance in constexpr functions, assigments and such.
The C++ Syntax is;
logical-or-expression ? expression : assignment-expression
It's used as;
condition ? condition_is_true_expression : condition_is_false_expression
That is, if condition evaluates to true, the expression evaluates to condition_is_true_expression otherwise the expression evaluates to condition_is_false_expression.
So in your case, result would always be assigned the value 1.
Note 1; A common mistake that one makes while working with the conditional operator, is to forget that it has a fairly low operator precedence.
Note 2; Some functional languages doesn't provide this operator, as they have expression 'if...else' constructs, such as OCaml;
let value = if b then 1 else 2
Note 3; A funny use case, which is perfectly valid is using the conditional operator, to decide, which of two variable to assign a value to.
(condition ? x : y) = 1;
Notice the parentheses are necessary, as this is really what you get without them;
condition ? x : (y = 1);
They are called shorthand if-else or ternary operators.
See this article for more information.