This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C preprocessor and concatenation
I have the macro
#define BUS B
I want to make macro BUS_PORT that expands to PORTB.
I did following:
#define BUS_PORT PORT ## BUS
But BUS_PORT expands to PORTBUS. What I did wrong? How to make it right?
As explained in this answer, you need an extra level of indirection. E.g.
#define BUS B
#define PASTER(x,y) x ## y
#define EVALUATOR(x,y) PASTER(x,y)
#define BUS_PORT EVALUATOR(PORT, BUS)
Related
This question already has answers here:
How to force macro to not expand
(1 answer)
Pre-processor: Expand a macro only once
(3 answers)
C: is it possible to expand marcos before pasting, but not recursively?
(1 answer)
Closed 9 months ago.
Given macro definitions like this:
#define GPIOA ((1234)+42)
#define GPIOB ((1234)+84)
#define LEDA_GPIO_Port GPIOA
#define LEDB_GPIO_Port GPIOB
#define MY_PORT_GPIOA 0
#define MY_PORT_GPIOB 1
I would like to define a macro MY_PORT which, given LEDA as argument somehow substitutes this to LEDA_GPIO_Port, then to GPIOA, turns that into MY_PORT_GPIOA and this into 0. So MY_PORT(LEDA) should evaluate to 0 and MY_PORT(LEDB) to 1. If the macros GPIOA/GPIOB wouldn't exist, this would be easy, but since they are defined as complex expressions (starting with parentheses) only one substitution must be performed, and the occurence of GPIOA must not be substituted by that expression.
I tried
#define MY_PORT2(port) MY_PORT_##port
#define MY_PORT(name) MY_PORT2(name##_GPIO_Port)
but here MY_PORT(LEDA) returns MY_PORT_LEDA_GPIO_Port, i.e. it lacks one substituion.
#define CONCAT(a,b) a##b
#define MY_PORT2(port) CONCAT(MY_PORT,port)
#define MY_PORT(name) MY_PORT2(name##_GPIO_Port)
does not compile, as it tries to concatenate the definition for GPIOA, i.e. one subtitution too many.
Is it even possible to do exactly one subtitution?
This question already has an answer here:
Macros not producing required result
(1 answer)
Closed 1 year ago.
#include <stdio.h>
#define SQRT(X)X*X
int main(){
int x=16/SQRT(4);
printf("%d",x);
return 0;
}
this output is 16 why? firstly i defined macro and then try do calculation
Currently int x=16/SQRT(4); will expand to int x=16/4*4; which is clearly 16. Use brackets to ensure the macro expansion is as intended:
#define SQRT(X) ((X)*(X))
Aside: SQRT is an odd name for the macro as that usually means "square root". SQR or SQUARE would be a more appropriate name.
This question already has answers here:
How to compare strings in C conditional preprocessor-directives
(14 answers)
Closed 2 years ago.
The following structure for C preprocessor directive #if works well.
#define C 1
#if C==1
...
#endif
But I'd like to use something like this:
#define REAL double
#if REAL==double
...
#elif REAL==float
...
#else
assert(0);
#endif
It's not valid. Is there a solution?
Is there a solution?
There is a whole world of ways you can combine macro expansion with conditional compilation. It's unclear which of them you would consider a solution, but here's a possibility:
#define doubledouble 1
#define floatfloat 1
#define concat_literal(x,y) x ## y
#define realtype(t1,t2) concat_literal(t1,t2)
// ...
#define REAL float
#if realtype(REAL, double)
// REAL is double
#elif realtype(REAL, float)
// REAL is float
#else
// REAL is something else
#endif
The realtype macro works by macro-expanding its arguments and concatenating them together. On rescan, if the result is doubledouble or floatfloat then that is expanded further to 1. If it is an identifier that is not defined as a macro name, then the #if treats it as 0.
It's not foolproof, of course. It does not work if REAL has a multi-token expansion, such as long double. There's also a minor risk of collision with other defined macro names.
This question already has answers here:
How can I concatenate twice with the C preprocessor and expand a macro as in "arg ## _ ## MACRO"?
(3 answers)
Closed 4 years ago.
I have something like :
#define NBR 42
#define THE_ANS_IS theAnsIsNBR
Currently the second macro is expand as 'theAnsIsNBR' as expected, but i want it to be expand as 'theAnsIs42' i am not sure if it is even possible !?
#define Paste(x, y) x##y
#define Expand(x, y) Paste(x, y)
#define NBR 42
#define THE_ANS_IS Expand(theAnsIs, NBR)
#define _CONCAT(x,y) x ## y
#define CONCAT(x,y) _CONCAT(x,y)
#define NBR 42
#define THE_ANS_IS CONCAT(theAnsIs, NBR)
This works because ## concatenates two tokens. The problem is, they aren't expanded first. But calling another macro on them expands them, therefore you need to nest two function-like macros here.
This question already has answers here:
Macro vs Function in C
(11 answers)
Closed 6 years ago.
Please explain the output — I am getting the output as 20,
but it should be 64 if I am not wrong.
#include<stdio.h>
#define SQUARE(X) (X*X)
main()
{
int a, b=6;
a = SQUARE(b+2);
printf("\n%d", a);
}
The correct result is 20.
Macros are simple text substitutions.
To see that the result is 20, just replace the X with b+2. Then you have:
b+2*b+2
as b is 6 it is
6+2*6+2
which is 20
When using macros it is important to use parenthesis so the macro should look
# define SQUARE(X) ((X)*(X))
Then the result will be 64 as the evaluation is
(b+2)*(b+2)
(6+2)*(6+2)
8*8
64