[duplicate] asked 12 years, 4 months ago modified 2 years, 9 months ago viewed 240k times I have been seeing code like this usually in the start of header files #ifndef headerfile_h #define headerfile_h and at the end of the file is #endif what is the purpose of this? How do i define a function with optional arguments Asked 13 years, 8 months ago modified 1 year, 4 months ago viewed 1.2m times I am trying to write something like this
#define cov_on(x) \ #ifdef coverage_tool \ _pragma (coverage #x) #endif is there any way to define cov_on like this I know what i have done above is wrong as i can't have #ifdef inside #define (# is not an allowed character in #define) So is there any solution? The #define directive is a preprocessor directive The preprocessor replaces those macros by their body before the compiler even sees it
A const variable declaration declares an actual variable in the language, which you can use.well, like a real variable Take its address, pass it around, use it, cast/convert it, etc #define simply substitutes a name with its value Furthermore, a #define 'd constant may be used in the preprocessor You can use it with #ifdef to do conditional compilation based on its value, or use the stringizing operator # to get a string with its value. Is it better to use static const variables than #define preprocessor
Or does it maybe depend on the context What are advantages/disadvantages for each method? I know that this is a long time after the original query, but this may still be useful This can be done in gcc using the stringify operator #, but it requires two additional stages to be defined first #define xstr(x) str(x) #define str(x) #x the value of a macro can then be displayed with #pragma message the value of abc
Which one is better to use among the below statements in c Static const int var = 5 Or #define var 5 or enum { var = 5 };
OPEN