Define a macro with defined(X)

Related searches

Here is a macro I want to use, if X_DEFINED is defined it will be evaluated to DEFAULT_X otherwise it will be evaluated to x

#define GET_X(x) (defined(X_DEFINED) ? DEFAULT_X : x)

It doesn't compile, with the error

error: 'X_DEFINED' was not declared in this scope

Any suggestions? I want to be able to select between a parameter and a global variable based on if X_DEFINED was defined or not

defined() only works in #if and similar preprocessor directives.

You want something like this:

#ifdef X_DEFINED
#define GET_X(x) DEFAULT_X
#else
#define GET_X(x) x
#endif

X Macro, X Macros are a technique for reliable maintenance of parallel lists, of code or data, whose Each expansion of LIST is preceded by a definition of X with the syntax for a list element. The invocation of LIST expands X for each element in the list. Macros are treated as a token substitution early in the compilation process. This means that large (or repeating) sections of code can be abstracted into a preprocessor macro. // This is an object-like macro #define PI 3.14159265358979 // This is a function-like macro.

You need to define 2 different macros, depending on whether X_DEFINED is defined:

#ifdef X_DEFINED
#   define GET_X(x) x
#else
#   define GET_X(x) DEFAULT_X
#endif

X-Macros in C, X-Macros are based on the property of nested macros and the ability to Each expansion of VARIABLES is preceded by a definition of X with� macro (array[x = y, x + 1]) passes two arguments to macro: array[x = yand x +1]. If you want to supply array[x = y, x + 1]as an argument,you can write it as array[(x = y, x + 1)], which is equivalent Ccode. All arguments to a macro are completely macro-expanded before they aresubstituted into the macro body.

Sloppy speaking you are mixing runtime stuff (evaluation of a ternary operator) with stuff that happens even before compilation (preprocessor). You can use #ifdef instead:

#ifdef X_DEFINED
    #define GET_X(x) DEFAULT_X
#else
    #define GET_X(x) x
#endif

Macro Arguments (The C Preprocessor), To define a macro that uses arguments, you insert parameters between the pair of parentheses in the macro definition that make the macro function-like. + 1] as an argument, you can write it as array[(x = y, x + 1)] , which is equivalent C code. #define directive (C/C++) 08/29/2019; 4 minutes to read +1; In this article. The #define creates a macro, which is the association of an identifier or parameterized identifier with a token string. After the macro is defined, the compiler can substitute the token string for each occurrence of the identifier in the source file. Syntax

The C Preprocessor: Macros, defines a macro named BUFFER_SIZE as an abbreviation for the token 1024 . passes two arguments to macro : array[x = y and x + 1] . A recursive macro is not fully expanded. For example, the definition . #define x(a,b) x(a+1,b+1) + 4. expands . x(20,10) to . x(20+1,10+1) + 4. rather than trying to expand the macro x over and over within itself. After the macro x is expanded, it is a call to function x(). A definition is not required to specify replacement tokens.

Function-like macros, More complex than object-like macros, a function-like macro definition declares the names rather than trying to expand the macro x over and over within itself. The macro is defined only within a function. The __FUNCTION__ macro isn't expanded if you use the /EP or /P compiler option. For an example of usage, see the __FUNCDNAME__ macro. _INTEGRAL_MAX_BITS Defined as the integer literal value 64, the maximum size (in bits) for a non-vector integral type. This macro is always defined.

Then, you can execute the second macro using ‘C-x e’ and execute ‘my-macro’ as if it were a standard Emacs command: ‘M-x my-macro’. If you also name the second macro, then you can define a third one… Keyboard macros are only defined for your current Emacs session.

Comments
  • suggestion: Let us know what you actually want to use this for so we can tell you how to do it without macros
  • btw this line alone does not produce any error. Please provide a minimal reproducible example
  • Re: "defined() only works in #if" -- indeed. And the code could be written with #if defined(X_DEFINED) as well as with #ifdef X_DEFINED.