Compiler bug ? Nonsensical result

I am an experienced programmer in Fortran, but new to Arduino.

if( ) seems to behave incorrectly. Had problem with a larger code, but boiled it down to make an essential example. Here is the entire example code, followed by the output that appears on the Serial Monitor.
Two things are strange -

  1. The value of modk seems to be correctly re-calculated the first time and second time through the loop, but is never recalculated on subsequent passes around the loop. modk is supposed to toggle between 0 and 1 every time through the loop.

  2. The action controlled by the if( ) statement (which is only a Serial.print) should only occur when modk=1, but, even though modk becomes zero and is stuck at zero as shown by the printed values each time through the loop (instead of toggling between 0 and 1), the action of the if( ) statement is activated nonetheless. Note that the variable kount (printed out) is increasing by 1 each time through the loop as would be expected.
    Any explanation ? Both 1) and 2) seem to make no sense.

      THE CODE :
    

int modk = 0;
int kount = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
kount = kount + 1;
modk = 1 - modk; // SHOULD TOGGLE modk BETWEEN 0 AND 1
Serial.print(" modk , kount = ");
Serial.print(modk);
Serial.print(" ");
Serial.println(kount);
if(modk = 1) {
Serial.println(" modk must be 1, but prints as 0");
}
Serial.println(" ");
delay(3000);
}

BELOW IS THE OUTPUT ON THE SERIAL.MONITOR.
NOTE THAT modk IS CORRECTLY CALCULATED FIRST TIME AND SECOND TIME, BUT THEN IS ALWAYS STUCK AT 0 (ACCORDING TO THE PRINT).
NEVERTHELESS, THE if( ) BEHAVES AS IF THE VALUE OF modk IS ALWAYS 1 AND NEVER CHANGES.

modk , kount = 1 1
modk must be 1, but prints as 0

modk , kount = 0 2
modk must be 1, but prints as 0

modk , kount = 0 3
modk must be 1, but prints as 0

modk , kount = 0 4
modk must be 1, but prints as 0

modk , kount = 0 5
modk must be 1, but prints as 0

. . . etc., endlessly with kount incrementing by 1 each time and modk always 0

You want "if (modk == 1){"

= is assignment
== is comparison

1 Like

= is for assignment, == is for comparison.
See the warning in the if structure reference.

Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

You can go back and fix your original post by highlighting the code and using ctrl-t and then clicking the </> in the menu bar.
code tags new

1 Like

The overwhelming majority of "compiler bugs" reported here are actually Pilot Error.

3 Likes

Me too, but very dated: Fortran 77.

If you find a good buy on aspirin, buy 'em as you will have many headaches from this thing called "Arduino." Under the Arduino is a scripted C++ compiler/linker.

Chips like the Espressif ESP32/ESP8266 use a C++ compiler-linker, Xtensa GNU compiler collection (GCC)

You'll have fun with this, on the 8-bit Arduino hardware, int is 16-bit but on the 32-bit architecture (Espressif, STM32) an int is 32-bit.

Oh, welcome to the forum!

Ray

Me too, but Fortran IV on an IBM 360 main frame.

1 Like

As an EE student, I used Fortran 66. Years later, I used Fortran 77 to model financial transactions on a Honeywell. All my Financials were tapes from the IBM-like 9903 (Formation.)

Me three.

That is an understatement. :expressionless:

It is advised to turn up the compiler warnings in the IDE and heed the advice you get from doing.

Good job on making a minimum example.

a7

Got you beat. Fortran 2.

Try:
modk ? modk = 0 : modk = 1;
C++ ternary operator
OR:

if (modk == 1)
  modk = 0;
else
  modk = 1;

RayL and groundF -
Thanks to both of you guys. That's does it. Just beginning to learn here.
Thanks again.

Tell me you're a FORTRAN programmer, without telling me you're a FORTRAN programmer. :smiley:

1 Like

If you think there is a compiler bug, 99.999999999% there isn’t

1 Like

A long time ago I came to the conclusion that great care is needed if you are going to claim a 'bug'.

A lot of research and experimentation needed before claiming a 'bug'.

I have found two real 'bugs' in my life, one a flaw in the design of the Rockwell 6522 VIA and another 'missunderstanding' that causes LoRa packets with errors to be accepted as good.

You old. :expressionless:

The Rockwell 6522 Versatike Interface Adapter.

Is this a flaw you found before several hundred thousand copies were made?

a7

No, the flaw was found after they were made.

I was looking after the Electronics side of the manufacture of the BBC micro at the time and the problem appeared as if out of no-where. It caused a failure rate of BBC Micros in the 5%-10% region causing major issues for manufacturing.

Toggle modk:
modk = !modk;

I knew that! :grinning: and:
modk ^= 1;
And how many more?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.