Why almost always capital letters are used when defining macros or variables like

#define SOMETHING 432

instead of

#define something 3243

Also, why in macro definition x is used for example

#define macro(x) (x*x)

Does the letter matter, and can longer variable names be used like

#define MACROSQUARE(variable) variable*variable

Some people like to keep their macros upper case, so they won't be confused with variable names. But generally that doesn't matter. It also doesn't matter whether you use long names for macro 'parameters'.

When you use macros in your code, preprocessor does this:
Original code:

#define SQUARE(blabla, blablabla) (blabla * blablabla)
#define NUMBER 12

// ... somewhere in your code
int a = 2, b = 6;
bool equal = (NUMBER == SQUARE(a, b));
// ...

After preprocessing:

// ... somewhere in your code
int a = 2, b = 6;
bool equal = (12 == (a * b));
// ...
commented: thanks +6

It is important to remember that MACROs and #defines completely ignore namespaces and are thus very prone to name-clashes if used carelessly.

So, the convention that is the most wide-spread in all C/C++ development is to use all uppercase for MACROs and #defines. This serves as a clear separation between what is under namespace and other scoping rules, and what is not. Also, it is typical and recommended to prepend a "namespace" to the MACROs and #defines, like so:

#define MY_LIBRARY_SQUARE(x) x*x

This is to make sure that your SQUARE macro doesn't conflict with some other library's SQUARE macro (in case one exists and doesn't have a more unique name).

>>But generally that doesn't matter.

I beg to differ. I, like most programmers, want to know for sure which things are MACROs and which are functions, which are #defines and which are constants. This convention of all uppercase is probably the one and only convention that everyone obeys without exception. I do think it matters. Of course, it doesn't matter to the compiler's pre-processor whether you use all uppercase or not, but it matters to every single C/C++ programmer out-there who might be looking at or using your code.

Why almost always capital letters are used when defining macros or variables like

#define SOMETHING 432

instead of

#define something 3243

Macros are problematic in that they're a preprocessor effect. History has shown that macros are both error prone and difficult to debug due to the text replacement of the macro invocation in your source code with the macro expansion in the intermediate translation unit. The compiler doesn't recognize macros because at that point the macros no longer exist.

The upper case naming convention is designed so that macros stand out. If you get an error on line N that's confusing and see an upper case name, that immediately tells you that the error is likely from the macro expansion. If the name weren't so obviously a macro, it might take longer to troubleshoot.

Also, why in macro definition x is used for example

#define macro(x) (x*x)

Does the letter matter, and can longer variable names be used like

#define MACROSQUARE(variable) variable*variable

It doesn't matter, you can use whatever you think is easiest for readers to understand. Much like i as a loop counter, x in macros is simply something that the majority gravitates toward because it's well understood.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.