0

Originally my code was:

#ifndef 2DO_H   
#define 2DO_H  
int ReadNumber();  
void WriteAnswer(int Nsumber1, int Number2);  
#endif

However I was getting an error #if[n]def expected an identifier. So I played around with it and realized that my error was in 2DO_H. When I changed my code to:

#ifndef DO_H   
#define DO_H  
int ReadNumber();  
void WriteAnswer(int Nsumber1, int Number2);  
#endif

It worked in the above case because I changed 2DO_H to DO_H. Why is it that when I have an extra number in front of the identifier, I get an error?

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
jlcv
  • 1,688
  • 5
  • 21
  • 50

2 Answers2

3

Because identifiers aren't allowed to start with a digit. This is covered in 2.11 Identifiers of the current C++ 11 standard, specifically the syntax section:

identifier:
    identifier-nondigit               # No digit allowed at front here.
    identifier identifier-nondigit    # Nor here.
    identifier digit                  # Nor here.
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • But is there a reason for this? Why is it that in C++ this happens? There has to be a reason for this because right now I don't see why have numbers in front of an identifier is bad in a programming language. – jlcv Mar 15 '13 at 06:26
  • 3
    @Justin, the reason why it's in C++ is because it was in C (C++ was originally just a layer on top of C). The reason why it's in C probably has to do with the fact that it's easier to keep numbers and identifiers distinct if you can tell the difference _early._ This greatly eases the task of the compilers (parsers/lexers) which, back when C was young, weren't that clever :-) Think of the identifier `3141592653589x` which you wouldn't know is not a integer until you get to the last character. – paxdiablo Mar 15 '13 at 06:28
  • 2
    @Justin and the situation gets a lot more complicated when also considering literals such as 0x3F, which is, in fact, a number. – JSQuareD Mar 15 '13 at 06:42
3
#ifndef 2DO_H

Nah.

#ifndef TODO_H

instead. An identifier can't begin with a digit.