1

In the code I'm writing, I have been told to define a variable in a header file in the following way:

#define CLR_BLACK 0x0000

and since this is the only example I've been given, I was wondering whether all variables defined in a header file with the #define command need to be in caps. For example, would the following be valid?

#define videoBuffer (u16*)0x6000000
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
superdemongob
  • 248
  • 5
  • 13

3 Answers3

6

No. You can use any combination of alphanumeric characters and underscores. Don't start with a number.

However a variable name like videoBuffer would be difficult to distinguish from regular variables (without syntax coloring). That's why most people either use all caps for preprocessor macros or start them with a lower case k, like this: kMyPreprocessorMacro

EDIT: Those are not "global variables" by the way (as you tagged). They're preprocessor macros. Basically an automatic find and replace mechanism that is run at compile time.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • so that videoBuffer example there would in fact be vaild? – superdemongob Nov 03 '12 at 01:23
  • Yes it would be valid. However a variable name like that would be difficult to distinguish from regular variables (without syntax coloring). That's why most people either use all caps for preprocessor macros or start them with a lower case `k` like that: `kMyPreprocessorMacro`. – DrummerB Nov 03 '12 at 01:25
  • right, thanks for that additional clarification on preprocessor macros. – superdemongob Nov 03 '12 at 03:24
0

No.

#define is a pre-processor macro. It replaces every occurrence of the first string after it with whatever comes after the string. The first string does not need to be in caps.

GraphicsMuncher
  • 4,583
  • 4
  • 35
  • 50
0

No, but it's a common and useful convention so if you're reading the code you can see what's a macro and what isn't. See C++ #ifndef for include files, why is all caps used for the header file?

Community
  • 1
  • 1
John Carter
  • 53,924
  • 26
  • 111
  • 144