Stray character in comment affects following line

In the following code fragment, I get the compilation errors below.

The single reverse slash at the end of the const declaration for RED_LED_PIN causes the following line to be treated as a comment.

If I simply delete that slash or even just add a space after it, it compiles just fine.

Anyone know what's going on with that?

Thanks,
.Andy

// pins used
const int HEARTBEAT_PIN = LED_BUILTIN;  // on board LED for heartbeat
const int RX_PIN = 2;                   // for xBee RX
const int TX_PIN = 3;                   // for xBee TX
const int RED_LED_PIN = 4;              // \
const int GRN_LED_PIN = 5;              //  > RGB LED for door state
const int BLU_LED_PIN = 6;              // /
const int BUTTON_PIN = 7;               // button for mode changes
const int BUZZER_PIN = 8;               // Piezo for UI confirmations
const int SENSOR_PIN = 9;               // magnetic sensor for door state
const int MSG_LED_PIN = A5;             // LED for UI confirmations

void setup() {
  // initialize all pins
  pinMode(RX_PIN, INPUT);
  pinMode(TX_PIN, OUTPUT);
  pinMode(MSG_LED_PIN, OUTPUT);
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(GRN_LED_PIN, OUTPUT);
  pinMode(BLU_LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(SENSOR_PIN, INPUT_PULLUP);
}

void loop() {}

/private/var/folders/_l/ld1sdgpx2nl4jgn8l6txc4mw0000gn/T/.arduinoIDE-unsaved2024111-90783-1kgihyw.1sf0k/sketch_dec1a/sketch_dec1a.ino: In function 'void setup()':
/private/var/folders/_l/ld1sdgpx2nl4jgn8l6txc4mw0000gn/T/.arduinoIDE-unsaved2024111-90783-1kgihyw.1sf0k/sketch_dec1a/sketch_dec1a.ino:19:11: error: 'GRN_LED_PIN' was not declared in this scope
pinMode(GRN_LED_PIN, OUTPUT);
^~~~~~~~~~~
/private/var/folders/_l/ld1sdgpx2nl4jgn8l6txc4mw0000gn/T/.arduinoIDE-unsaved2024111-90783-1kgihyw.1sf0k/sketch_dec1a/sketch_dec1a.ino:19:11: note: suggested alternative: 'MSG_LED_PIN'
pinMode(GRN_LED_PIN, OUTPUT);
^~~~~~~~~~~
MSG_LED_PIN

exit status 1

Compilation error: 'GRN_LED_PIN' was not declared in this scope

  • That’s because a back slash \ tells compiler to go down to the next line and keep on compiling (which makes the next line part of the comment).
    // . . .
2 Likes

As noted by @LarryD a \ followed immediately by an end of line is the C and C++ line continuation character. Adding a space indicates that it is not a continuation character.

You could perhaps do this instead:

// ⎫
// ⎬ RGB LED for door state
// ⎭

Thanks all. I've been programming in C/C++ for about a hundred years and have never run across this feature!

.Andy

With current code editors that have scrolling and soft line wrap formatting it isn't used very often.

The "wrong slash" let 40-char wide screens show the multi-line of code split at a desired place rather than forced at the 40th char.

These days it's most often seen when defining multi-line macros (of course is macros in general are "frowned upon", especially in C++, and this should often be an inline function instead):

#define DEBUGFLAGGED(flag, args) do {                         \
                                   if (debugflags & flag) {   \
                                     Serial.print(args);      \
                                   }                          \
                                 } while (0)

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