21

I have some experience in programming in C but I would not dare to call myself proficient. Recently, I encountered the following macro:

#define CONST(x) (x)

I find it typically used in expressions like for instance:

double x, y;
x = CONST(2.0)*y;

Completely baffled by the point of this macro, I extensively researched the advantages/disadvantages and properties of macros but still I can not figure out what the use of this particular macro would be. Am I missing something?

3
  • 2
    This macro achieves nothing, other than perhaps as a visual reminder that something is a constant... Jan 31, 2013 at 15:53
  • 2
    The only reason I can think of to do this would be to allow an external tool to more easily find specific constants in the program. It's used for marking text to be translated sometimes although I'm not sure how it would help here
    – jcoder
    Jan 31, 2013 at 15:56
  • 2
    And it makes it searchable
    – Minthos
    Jan 31, 2013 at 15:56

7 Answers 7

11

As presented in the question, you are right that the macro does nothing.

This looks like some artificial structure imposed by whoever wrote that code, maybe to make it abundantly clear where the constants are, and be able to search for them? I could see the advantage in having searchable constants, but this is not the best way to achieve that goal.

It's also possible that this was part of some other macro scheme that either never got implemented or was only partially removed.

5
  • 1
    +1, that's the only sane thing I can think of for this as well. Jan 31, 2013 at 15:55
  • 5
    That is not sane, a sane programmer wouldn't use "magic numbers" but instead #define SOME_FLOAT 2.0, followed by x = SOME_FLOAT*y;. Or even better, use a const variable, if applicable.
    – Lundin
    Jan 31, 2013 at 15:57
  • 2
    A const variable is not 'even better'. It's Considered Harmful. Jan 31, 2013 at 16:17
  • @R.. what? would you care to explain? I don't want to be laboring under the false impression that const ints are preferred over defines (in C++. is it different in C?), so any light you can shed on this would be wonderful, thanks! Jan 31, 2013 at 16:54
  • It's very different in C. const variables are not constant expressions. In C you should always use macros or enums for symbolic constants. Jan 31, 2013 at 17:04
6

Some (old) C compilers do not support the const keyword and this macro is most probably a reminiscence of a more elaborate sequence of macros that handled different compilers. Used like in x = CONST(2.0)*y; though makes no sense.

You can check this section from the Autoconf documentation for more details.

EDIT: Another purpose of this macro might be custom preprocessing (for extracting and/or replacing certain constants for example), like Qt Framework's Meta Object Compiler does.

2

There is absolutely no benefit of that macro and whoever wrote it must be confused. The code is completely equivalent to x = 2.0*y;.

1

Well this kind of macro could actually be usefull when there is a need to workaround the macro expansion.
A typical example of such need is the stringification macro. Refer to the following question for an example : C Preprocessor, Stringify the result of a macro

Now in your specific case, I don't see the benefit appart from extreme documention or code parsing purposes.

1
  • Good point about the stringification macro which I also encountered in my research in an even earlier SO post stackoverflow.com/questions/1597007/… However in the code I found , it is not used in that manner
    – Pankrates
    Jan 31, 2013 at 16:12
0

Another use could be to reserve those values as future function invocations, something like this:

 /* #define CONST(x) (x) */
 #define CONST(x) some_function(x)

 // ...

 double x, y;
 x = CONST(2.0)*y; // x = some_function(2.0)*y;
0

Another good thing about this macro would be something like this

result=CONST(number+number)*2;

or something related to comparisons

result=CONST(number>0)*2;

If there is some problem with this macro, it is probably the name. This "CONST" thing isn't related with constants but with some other thing. It would be nice to look for the rest of the code to know why the author called it CONST.

0

This macro does have the effect of wrapping parenthesis around x during the macro expansion.

I'm guessing someone is trying to allow for something along the lines of

CONST(3+2)*y

which, without the parens, would become

3+2*y

but with the parens becomes

(3+2)*y

I seem to recall that we had the need for something like this in a previous development lifetime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.