"expected unqualified-id before numeric constant" error with certain names

This is the code

//Screens pinnumbers with CS number:
byte CS1 = 34;
byte CS2 = 36;
byte CS3 = 40;
byte CS4 = 42;
byte CS5 = 46;
byte CS6 = 43;
byte CS7 = 37;
byte CS8 = 35;
byte CS9 = 33;
byte CSx10 = 49;
byte CSx11 = 48;
byte CSx12 = 47;
byte CS13 = 44;
byte CS14 = 45;
byte CS15 = 41;

void setup() {

}

void loop() {

}

An the error:

Arduino: 1.8.13 (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

In file included from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\iom2560.h:38:0,

                 from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\io.h:174,

                 from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\pgmspace.h:90,

                 from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:28,

                 from sketch\test.ino.cpp:1:

test:11:6: error: expected unqualified-id before numeric constant

 byte CS10 = 49;

      ^

test:12:6: error: expected unqualified-id before numeric constant

 byte CS11 = 48;

      ^

test:13:6: error: expected unqualified-id before numeric constant

 byte CS12 = 47;

      ^

exit status 1

expected unqualified-id before numeric constant

It can compile without errors if I replace CS10, CS11 and CS12 with any other name. e.g.: CSx10, CSx11 and CSx12

It seems like it doesn't like me to use the names CS10, CS11 and CS12. Why is that?

This is an excerpt of a program that has been given to me, and it is working for the owner (I cannot ask him for details now)

It seems like it doesn't like me to use the names CS10, CS11 and CS12. Why is that?

most likely because those names have already been #defined as something.
It seems to me that anyway you should be using an array to hold the pin numbers.

There is no hard and fast rules but variables whose name is in capitals are generally constants or macros and that includes those defined by the Arduino system itself

In this case it would appear that CS10 and the other troublesome names are already defined

Try printing CS10, CS11 and CS12 without even declaring them and you will see values of 0, 1 and 2. So, when the compiler sees

byte CS10 = 49

and CS10 is replaced by 0 it becomes

byte 0 = 49;

hence the error

Deva_Rishi:
most likely because those names have already been #defined as something.
It seems to me that anyway you should be using an array to hold the pin numbers.

I have deleted all the code except the excerpt that I have posted here, but I still got the error.

As I said

Try printing CS10, CS11 and CS12 without even declaring them and you will see values of 0, 1 and 2.

OK, so they are internally used by Arduino. I understand. Thank you!

The issue is solved easily choosing other names. I was just curious to know why this happens.

Here are the definitions of CS10, CS11, CS12 in the avr-libc component of the AVR toolchain:

#define CS12    2
#define CS11    1
#define CS10    0

One of the things I think is really cool about Arduino IDE 2.x is it has this "Go to definition" option in the context menu. So you can right click on some part of the code you're curious about and then take a look at its source code! I think it can be a really valuable learning tool. This is a feature we take for granted in advanced IDEs for other languages, but it has not been available at this level of functionality for the Arduino language before now.

With the classic Arduino IDE, my trick for investigating the source of macro definitions was to define it to some value I knew would be different from the original in order to trigger a redefinition warning, which shows the location of the original definition. If you compile this sketch with compiler warnings enabled in File > Preferences:

#define CS10 foo
void setup() {}
void loop() {}

You get a warning something like this in the black console pane at the bottom of the Arduino IDE window:

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_268986\sketch_apr04a.ino:1:0: warning: "CS10" redefined
 #define CS10 foo
 
In file included from c:\users\per\appdata\local\arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7\avr\include\avr\iom2560.h:38:0,
                 from c:\users\per\appdata\local\arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7\avr\include\avr\io.h:174,
                 from c:\users\per\appdata\local\arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7\avr\include\avr\pgmspace.h:90,
                 from C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3\cores\arduino/Arduino.h:28,
                 from C:\Users\per\AppData\Local\Temp\arduino_build_68419\sketch\sketch_apr04a.ino.cpp:1:
c:\users\per\appdata\local\arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7\avr\include\avr\iomxx0_1.h:722:0: note: this is the location of the previous definition
 #define CS10    0

Thank you for the information!!

Now I know why it was working for the owner of the program: he wrote it for Arduino Due, and I had Arduino Mega selected.

If I change the board to Arduino Due, I can use CS10, CS11 and CS12 without issues

CS10, CS11, and CS12 are the names of the three "Clock Select" bits in TCCR1B (Timer/Counter Control Register B for Timer/Counter 1). Similarly CS20, CS21, and CS22 are for Timer2 and CS00, CS01, and CS02 are for Timer0.

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