Arduino unexpected error messages in wprogram.h

When I compile the following code, I get the error messages indicated in the comments:

#define c 3830 // 261 Hz
const int speakerOut = 9;
void setup() {
}
void loop() {
}
/*
Produces these messages...

In file included from N:\HyperStudioStacks\Arduino\arduino-0022\hardware\arduino\cores\arduino/WProgram.h:13,
from testbug1.cpp:5:
N:\HyperStudioStacks\Arduino\arduino-0022\hardware\arduino\cores\arduino/WCharacter.h:26: error: expected ',' or '...' before numeric constant
N:\HyperStudioStacks\Arduino\arduino-0022\hardware\arduino\cores\arduino/WCharacter.h:27: error: expected ',' or '...' before numeric constant

*/

This minimal souce came from a file that was developed (by someone else) under Arduino-0020. I am using Arduino-0022.

When I add

#include <wprogram.h>

before the define statement, it works as expected. Does anyone have an explaination for why these error messages get produced when Arduino.exe automatically pulls in wprogram.h?

Does anyone have an explaination for why these error messages get produced when Arduino.exe automatically pulls in wprogram.h?

Yes. It is a matter of WHERE the Wprogram header file gets included. There is a flaw in the IDE in where it inserts the stuff it adds to the sketch. Having #define statements confuses the IDE, and where it includes the header file.

You can hold down the shift key to see where the .cpp file that the IDE produces is, and have a look at that.

The #define is killing you, though. Everywhere where the letter c is in your sketch is replaced with 3830. So, const becomes 3830onst.

By convention, #define'd names are all capital letters. In addition, one letter names are generally not advised.

Thanks for the feedback. The source I used was from another user, built under an earlier version of the IDE (0020). For now, insertion of the include statement corrected the issue.

For now, insertion of the include statement corrected the issue.

You misspelled "masked". You still need to fix the #define statement.

he #define is killing you, though. Everywhere where the letter c is in your sketch is replaced with 3830. So, const becomes 3830onst.

Errr, no.
Everywhere you have a complete lexical token that is a single 'c', it wll be replaced by 3830, but the 'c' in "const" is part of the token "const", so will not be replaced.

It would cause you trouble would be if you declared a variable called "c".

Here is the reference to the original code:

That is on the arduino.cc tutorial page. Hmmm. Did some bad coding get through?

Here is the full set of questionable includes:

// TONES ==========================================
// Start by defining the relationship between
// note, period, & frequency.
#define c 3830 // 261 Hz
#define d 3400 // 294 Hz
#define e 3038 // 329 Hz
#define f 2864 // 349 Hz
#define g 2550 // 392 Hz
#define a 2272 // 440 Hz
#define b 2028 // 493 Hz
#define C 1912 // 523 Hz
// Define a special note, 'R', to represent a rest
#define R 0

Here is where those defined are used:

// MELODY and TIMING =======================================
// melody[] is an array of notes, accompanied by beats[],
// which sets each note's relative length (higher #, longer note)
int melody[] = { C, b, g, C, b, e, R, C, c, g, a, C };
int beats[] = { 16, 16, 16, 8, 8, 16, 32, 16, 16, 16, 8, 8 };
int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.

I don't have 0022 - what does Wcharacter.h look like?
It seems likely that one of those frequency #defines is being misinterpreted.

OK, got 0022

Just a guess:

N:\HyperStudioStacks\Arduino\arduino-0022\hardware\arduino\cores\arduino/WCharacter.h:26: error: expected ',' or '...' before numeric constant

line 26 is inline boolean isAlphaNumeric(int c) __attribute__((always_inline));

You see:

inline boolean isAlphaNumeric(int 3830) __attribute__((always_inline));

doesn't work.