user42133: I'm doing a simple calculation with integers (on Arduino with ESP8266 12E), but I can't get the expected result and can't find the error. Can someone guide me?
#define A      200
#define B      A * 62
#define C      500

void setup() {
   Serial.begin(9600);
   Serial.println("");

   unsigned long aux = 0;

   aux = (B * 500) / C;    // (12400 * 500) / 500 = 12400
   Serial.printf("aux = %d\n", aux);
   aux = aux * C;          // 12400 * 500 = 6200000
   Serial.printf("aux = %d\n", aux);

   // ERROR: Should result in "500", but is resulting in "1922000"
   aux = aux / B;          // 6200000 / 12400 = 500

   Serial.printf("aux = %d\n", aux); // It's printing "1922000"
}


user1621: Fully-parenthesizing macros (as noted in answer by dmz) solves one class of problem.
Another thing you should do is, in any arithmetic expression which involves literal constants, use the L suffix on at least one of the constants involved if there's any chance the result will exceed 32767 (the maximum guaranteed-representable value for int). The type of an arithmetic operation in C is based on the types of the operands of that operation only; the type of the variable to which the result is assigned is irrelevant.
(edit)
For example:
long q, r;
unsigned char v = 231;
unsigned char w = 197;

q = v * w * 5;
r = v * w * 5L;

q might contain 30927, since after the Usual Arithmetic Conversions (6.3.1.8) [in this case, the Integer Promotions (6.3.1.1-2)] all operands are of type int and it's possible for INT_MAX to be 32767 in which case each operation would be performed modulo 32768.
r will contain 227535, since the constant 5L is of type long and thus all operations in this expression will be performed on values of type long.

user-1: In your #define of B you missed parenthesis (). Change your definition to:
#define B      (A * 62)

Without parenthesis you first divide 6200000 by 200 and then multiply result by 62, which is not what you intend.

42133: <p>I'm doing a simple calculation with integers (on Arduino with ESP8266 12E), but I can't get the expected result and can't find the error. Can someone guide me?</p>

<pre><code>#define A      200
#define B      A * 62
#define C      500

void setup() {
   Serial.begin(9600);
   Serial.println("");

   unsigned long aux = 0;

   aux = (B * 500) / C;    // (12400 * 500) / 500 = 12400
   Serial.printf("aux = %d\n", aux);
   aux = aux * C;          // 12400 * 500 = 6200000
   Serial.printf("aux = %d\n", aux);

   // ERROR: Should result in "500", but is resulting in "1922000"
   aux = aux / B;          // 6200000 / 12400 = 500

   Serial.printf("aux = %d\n", aux); // It's printing "1922000"
}
</code></pre>
 1621: <p>Fully-parenthesizing macros (as noted in <a href="https://electronics.stackexchange.com/a/414154/5185">answer by dmz</a>) solves one class of problem.</p>

<p>Another thing you should do is, in any arithmetic expression which involves literal constants, use the <code>L</code> suffix on at least one of the constants involved if there's any chance the result will exceed 32767 (the maximum guaranteed-representable value for <code>int</code>). The type of an arithmetic operation in C is based on the types of the operands of that operation only; the type of the variable to which the result is assigned is irrelevant.</p>

<p>(edit)
For example:</p>

<pre><code>long q, r;
unsigned char v = 231;
unsigned char w = 197;

q = v * w * 5;
r = v * w * 5L;
</code></pre>

<p><code>q</code> might contain 30927, since after the Usual Arithmetic Conversions (6.3.1.8) [in this case, the Integer Promotions (6.3.1.1-2)] all operands are of type <code>int</code> and it's possible for <code>INT_MAX</code> to be 32767 in which case each operation would be performed modulo 32768.</p>

<p><code>r</code> will contain 227535, since the constant <code>5L</code> is of type <code>long</code> and thus all operations in this expression will be performed on values of type <code>long</code>.</p>
 -1: <p>In your #define of B you missed parenthesis (). Change your definition to:</p>

<pre><code>#define B      (A * 62)
</code></pre>

<p>Without parenthesis you first divide 6200000 by 200 and then multiply result by 62, which is not what you intend.</p>
