%= operator missing in docs

I noticed all the other "compound operators" are listed, but %= is mysteriously missing. However, my code using it appears to compile without issue, so I do believe it's there.

er
dons steel hat
pops head over parapet

%= wot that do then?

mmcp42:
er
dons steel hat
pops head over parapet

%= wot that do then?

% is the modulo operator (returns the remainder of a division); thus if x = 5 % 2, then x = 1.

So, lets say you set x = 8, then you do x %= 3 (prefix notation) - then x = 2.

Ultimately, I think the OP is reading more into the manual than there is; in standard C/C++, there isn't anything called "compound operators", just operators. These operators can be combined with the assignment operator (=) in a pre- or post-fix format. I think part of the confusion is the fact that the manual only touches on this, but doesn't go in depth as would a proper tutorial on ANSI C/C++, which leads to this confusion (on this point and others - such as "missing" or "hidden" functions which are a part of the standard library) for those without an understanding of the base language.

Orum: You might do yourself a favor and look into general ANSI C/C++ tutorials on the internet and/or in dead-tree form; such will only assist you in your understanding of the Arduino as a whole...

ahh nods sagely
guess it's one of those things that you'll need one day

think I'll stick to "easier-to-read" rather than save a byte or two

thanks for the explanation though!

mmcp42:
ahh nods sagely
guess it's one of those things that you'll need one day

think I'll stick to "easier-to-read" rather than save a byte or two

thanks for the explanation though!

First off, I must apologize - I made a mistake: The notation "{var} {operator}= {value}" is actually a form of an assignment operation, and has nothing to do with pre- and post-fix notation, which is "{operator} {operator}{var}" and "{var}{operator} {operator}", respectively. For example:

int x = 0;

x += 1; // assignment, x = 1
++x; // pre-fix notation, x = 2
x++; // post-fix notation, x = 3

...however, you should know and understand pre- and post-fix notation, and how they work - because they can both be useful tools, even if they aren't necessarily as easy to understand. For instance, compare this example:

int i = 0;
while (i++ < 2) {
  serial.println(i);
}

...vs...

int i = 0;
while (++i < 2) {
  serial.println(i);
}

When you understand why these example don't give the same results, you will hopefully understand how pre- and post-fix notation can be useful...

:slight_smile:

Well, the main reason I brought it up was that all the other similar operators (+=, -=, *=, etc.) are listed, and %= is strangely missing. Normally who are familiar with the operator, myself included, just assume it is there. I was puzzled though when every other operator was listed, but %= was not, which is why I hope for the inclusion of %=.

mmcp42:
think I'll stick to "easier-to-read" rather than save a byte or two

Note that the bytes you save are only in your source code. Both should compile to the same object code. There is no performance benefit to the more compact version.

There's a lot to be said for easier to read. I've been programming for 30 years and "x%=3" would make me stop and scratch my heat.