3

I'm a student who got interested in computer science recently. I'm studying C++ because I am interested in embedded systems.

When I tried to test the operator /= on my own. I want to learn about it by doing. The code that I wrote was

int a /= --b + 3;

but the compiler gave me an error message. But when I modified it to

int a = 0;
a /= --b + 3;`

it worked well. I thought it is related to l-values and r-values. Why does the 1st example with operator /= give me an error but the 2nd example above is ok? Can I ask you for some reference to get a hint about this question?

PS: When I tested with

int t = t / (--a + 3);

it worked well too! What is the difference? Can you point me to some document about that?

Swordfish
  • 12,971
  • 3
  • 21
  • 43
정지수
  • 95
  • 1
  • 6
  • What would you expect the result to be? – molbdnilo Feb 06 '19 at 03:02
  • `int t=t/(--a+3);` does not "work well" – it has undefined behaviour because you're using the value of `t` before it has one. A decent compiler will warn you about this. – molbdnilo Feb 06 '19 at 03:03
  • Read about the [mcve]. I believe you can cut your example down to `int main() { int a /= 2; int b = 4; b /= 2; int c = c / 2;}`. (Think about what value the variable might have when you divide it in each case.) – molbdnilo Feb 06 '19 at 03:08
  • Thank you for your help! I will refer that document! Thanks! – 정지수 Feb 06 '19 at 03:48
  • About the result was: Expected: 0 (next line) 1 (next line) 0 Actual: the line "int a /= --b + 3;" displayed error that "there are no ; before /= " – 정지수 Feb 06 '19 at 04:01

2 Answers2

4

I would like to mention two things.

  • What is the meaning of this code?
  • Is it valid C++ syntax?

Let's take a look at both.

when I tested "int a/=--b+3", it has error but when I modified to "int a=0;

a/=--b+3;" , it works well.

Unlike Java, C/C++ does not automatically initialize integer's value by 0 and it contains a garbage value(called "indeterminate value" officially). So int a/=--b+3; is more like int a; a/=--b+3; which is still be a meaningless value.

And when you declare a variable, C/C++ grammar does not allow /=. Here are the ways for variable declaration and initialization. I'm not sure there is more ways.

  • int a = 1;
  • int a(1);
  • int a{1}; (since C++11)
Hanjoung Lee
  • 2,123
  • 1
  • 12
  • 20
  • 1
    You should probably have used the appropriate term "indeterminate value" and mentioned that reading an indeterminate value is undefined behaviour. – *I'm not sure there is more ways.* – `int a{};` ^^ – Swordfish Feb 06 '19 at 04:00
  • 1
    Thank you for your help! So it can be related with property of C++ and initialization! Thank you! – 정지수 Feb 06 '19 at 04:08
  • @Swordfish I had not heard about the official term "indeterminate value" before. Just updated my answer. Thanks! – Hanjoung Lee Feb 06 '19 at 04:13
  • [What is \[an\] Indeterminate Value?](https://stackoverflow.com/questions/13423673/what-is-indeterminate-value) – Swordfish Feb 06 '19 at 04:18
3
a /= b;

is the same as:

a = a / b;

so this means that this below statement makes no sense:

int a /= (--b + 3);

Because it's equivalent to:

int a = a / (--b + 3);

Assuming that b has already been defined here; the problem is that a hasn't been defined, and so can't be used as part of the assignment.

The problem here is the same as the problem with this statement:

int a = a;

This also explains why the following code does work:

int a = 0;
a /= (--b + 3);

Because it's equivalent to this:

int a = 0;
a = a / (--b + 3);

Because a is known in the second line above, the RHS can be defined, and the new value for a determined.

More generally, operators like /=, *=, +=, -= and %= shouldn't be used during initialisation of a variable. A compiler (such as g++) should respond with an error if you ever try to do this.

Elliott
  • 2,603
  • 2
  • 18
  • 35