Difference between -= and =- in C++ and C? [duplicate] - c++

This question already has answers here:
What does =+ (equals-plus) mean in C?
(7 answers)
Closed last year.
I often confuse both "-=" and "=-", like what is the exact difference between them?
int main()
{
int x=10, a=-3;
x=-a;
printf("%d",x);
return 0;
}
Output
3

-= is a compound assignment operator. =- is two operators applied seperately.
While
a =- 3;
is the same as
a = (-3);
This
x -= a;
is more or less equivalent to
x = x - a;
"More or less" because operators can be overloaded (in C++) and typically the compound operator avoids the temporary right hand side.
Btw you are using =- twice in your code while the questions asks for += vs =+. += is a compound operator as well.

Related

In C++, what does it mean when equals is used twice? [duplicate]

This question already has answers here:
double '=' in initialization
(3 answers)
Closed 3 years ago.
My apologies if this is a duplicate: searching for this isn't easy.
Example code taken from rtorrent:
m_bindings[KEY_UP] = m_bindings['P' - '#'] = std::bind(&ElementDownloadList::receive_prev, this);
What does the double value-setting mean, and how can this statement be explained?
The expression is evaluated from the right equals sign to the left. The statement a = b = c can be rewritten a = (b = c). The result of an = operation is the value that was assigned. Thus the result of (b = c) is c, making the next operation equivalent to a = c.
This is similar to x = y = 1 which is short hand for y = 1 and x = y.
This is called operator chaining. What you are doing is assigning the return value of the right hand operator = to the left hand operator =
It is equivalent to doing
m_bindings['P' - '#'] = std::bind(&ElementDownloadList::receive_prev, this);
m_bindings[KEY_UP] = m_bindings['P' - '#'];
but saves you a line of code. It also saves you from calling operator[] a second time, which could be expensive. Personally, I would use the 2 line version to make the code easier to read unless performance is really an issue.

c++ argument evaluation order for assignment? [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
In c++, arguments evaluation of order is not guaranteed, but is the order of left/right sub expression of assignment expression is guaranteed? For example
#include <iostream>
#include <map>
int main()
{
int i = 2;
std::map<int, int> map;
map[i++] = i--;
return 0;
}
Is left expression i++ guaranteed to be executed before right expression i--?
You asked:
Is left expression i++ guaranteed to be executed before right expression i--?
No, it is not.
The line
map[i++] = i--;
could end up being
map[2] = 3;
or
map[1] = 2;
depending on which side of the assignment operator gets evaluated first.
However, since the line invokes undefined behavior, it could also be, as pointed out in the comment by #juanchopanza, :
map[42] = -999;

Some C/C++ syntax [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 8 years ago.
I have done some searching, but didn't find the answer
The code:
char b = 'b';
char c = 'c';
char a[5] = "";
a[0] = b, c;
What last line means? The b, c part?
Thank you all
That uses the elusive comma operator to cause confusion.
It evaluates b, and result of that is then asssigned to a[0]. After that, c is evaluated but its value thrown away. At least this is the case in C.
The comma has lower precedence than assignment (see this handy table) which is extra confusing.

*buf++ = *buf + 10 - Explanation [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 9 years ago.
Example code
int arr[3] = { 0, 1 };
int* buf = arr;
*buf++ = *buf + 10;
Result of last expression is that buf[0] == 10. I taught it would be buf[0] == 11.
A college of mine wrote something similar to the example code and I taught it works differently than it does. And I would like to know why it works the way it does.
The way I went about figuring it out was to look at the operator precedence table. There it states that suffix ++ has precedence over dereference. Hence I taught that on the left of operator= buf would point to the first element, but on the right of the operator= it would have already been incremented and would point to the second element. However that is not the case.
My question is, why is that so? Preferably a standard quote :) However any explanation is welcome!
You are accessing and modifying the pointer multiple times in a single sequence point. This is undefined behavior.
More generally, reading from and writing to any variable between sequence points is undefined. The fact that you have a pointer in this specific example is by-the-by.
To avoid confusion with the pointer:
int i = 0;
i++ = i + 1; // UB
Logically, should the i on the right hand side be the "current" value of i, or the modified value? This is why it is undefined. Instead, separate the code:
int i = 0;
++i;
i = i + 1;
Which is clear, and well defined.

using % on a double in c++ [duplicate]

This question already has answers here:
Can't use modulus on doubles?
(4 answers)
Closed 9 years ago.
im trying to use the % operator on a double in c++, i have done the same in java and it works fine.
is there something im missing here or is this not allowed, sorry im new to c++ so might be making a really stupid error here
double i = full_price_in_pence / 100.0;
double j = full_price_in_pence % 100;
int final_pounds = (int) i;
int final_pence = (int) j;
and these are both double values
full_price_in_pence
full_price_in_pounds
You should use the std::fmod() function from the <cmath> Standard header:
#include <cmath>
// ...
double j = fmod(full_price_in_pence, 100);
% is for integers only, you're looking for fmod.
You cannot use % operator for a double variable. Only int variables are allowed to do that.
You can check some good answers from another question like this; you can find them here.
No, it's not allowed. Operands of the % operator must be of integral types. Use std::fmod() instead.