using macro with preprocessor directive #define I have written the following two codes.
first code passes the expression e.g. a+b
_valid_pagesize(a+b)
in second code
c=a+b
and then passed this c as an argument to macro.
_valid_pagesize(c)
the second code runs perfectly whereas the first code doesn't.
can we not pass expressions as arguments to macro like the way we can in case of a function..?
why not..?
the code passing expression as argument :
#include<stdio.h>
#include<stdlib.h>
#define _valid_pagesize(_newsize) (!(_newsize % 0x80000000)?1: \
(!(_newsize % 0x40000000)?1: \
(!(_newsize % 0x10000000)?1: \
(!(_newsize % 0x4000000) ?1:0))))
int main(int argc, char* argv[]){
uint64_t size[2];
size[0]=atoi(argv[1]);
size[1]=atoi(argv[2]);
if(_valid_pagesize(size[0]+size[1])){
printf("success\n");
}
return 0;
}
Rule #1 with macros is to enclose all arguments in parentheses, to avoid exactly the problem that you're seeing.
#define _valid_pagesize(_newsize) (!((_newsize) % 0x80000000)?1: \
(!((_newsize) % 0x40000000)?1: \
(!((_newsize) % 0x10000000)?1: \
(!((_newsize) % 0x4000000) ?1:0))))
When you pass a+b to the macro, it expands to
a + b % 0x40000000
and since % has higher precedence than +, you don't get the result you expect. By enclosing the argument in parentheses, the macro expands to
(a + b) % 0x40000000
which works as expected.
Macro parameters aren't evaluated prior to being passed to the macro, since macro expansion occurs prior to compilation. Instead, the actual expression is passed unchanged into the macro, and is substituted into all occurrences of the parameter name. Therefore, _valid_pagesize(a+b) expands to this:
(!(a+b % 0x80000000)?1: \
(!(a+b % 0x40000000)?1: \
(!(a+b % 0x10000000)?1: \
(!(a+b % 0x4000000) ?1:0))))
Now, the reason it produces wrong answers is hopefully obvious. Follow user3386109's advice about enclosing the parameter in parentheses.
Put _newsize inside braces in the macro expansion
Related
I would like to pass 2 arguments to a macro using another macro:
#define A_AND_B 5,1
#define ADD(a, b) a + b
int add_them(void)
{
int result = ADD(A_AND_B);
return result ;
}
I would hope that expands to
int result = 5 + 1;
and I get 6. Instead I get
Error 'ADD' undeclared (first use in this function)
Error macro "ADD" requires 2 arguments, but only 1 given
Is there a way around this?
As is often the case, you need an extra level of (macro) indirection:
#define A_AND_B 5,1
#define ADD(...) ADD_(__VA_ARGS__)
#define ADD_(a, b) a + b
int add_them(void)
{
int result = ADD(A_AND_B);
return result ;
}
ADD is defined as variadic so that it will work as either ADD(A_AND_B) or ADD(A, B).
This works because __VA_ARGS__ in the replacement body of ADD is replaced with the actual arguments before the replacement body is scanned for macros.
Per C 2018 6.10.3.1, a compiler first identifies the arguments for a function-like macro and then performs macro replacement on the arguments, followed by macro replacement for the function-like macro. This means that, in ADD(A_AND_B), the argument is identified as A_AND_B before it is replaced with 5,1. As the macro invocation has only this single argument and the macro is defined to have two parameters, an error is diagnosed.
Given your definition of ADD, there is no way to change this behavior in a compiler that conforms to the C standard.
You can instead use another macro to expand the arguments and apply the desired macro:
#define Apply(Macro, Arguments) Macro(Arguments)
then int result = Apply(ADD, A_AND_B); will work. That will identify ADD and A_AND_B as arguments to Apply. Then it will expand those, producing an unchanged ADD and 5,1. Then the macro replacement for Apply produces ADD(5,1). Then this is again processed for macro replacement, which replaces ADD(5,1) in the ordinary way.
Note that good practice is usually to define ADD as #define ADD(a, b) ((a) + (b)) to avoid unexpected interactions with other operators neighboring the use of the macro.
Please give me full description....
The first snippet of code has the 'function call' (macro invocation) before the increment operator, and second one has the function call after the increment operator.
#include <stdio.h>
#define square(x) x*x
int main()
{
int a,b=3;
a=square (b)++;
printf("%d%d",a,b);
return 0;
}
output:
124
why is 124 returned here
#include <stdio.h>
#define square(x) x*x
int main()
{
int a,b=3;
a=square (b++);
printf("%d%d",a,b);
return 0;
}
output:
125
and 125 here?
The thing to keep in mind is that macros provide simple substitution of preprocessor tokens. In particular, they may evaluate their arguments more than once, and if not guarded by parentheses, they may produce unintended reassociation.
In the first example, we have
a=square (b)++;
This expands to:
a=b*b++;
This is actually undefined behavior, since the b and b++ are unsequenced, and b++ modifies b. In your case, you are seeing 12 and 4 for a and b, so it would seem that the first value of b is picking up the incremented value, so you're getting 4*3, but you can't count on this behavior. The final value of b is 4 since it is incremented once.
In the second example, we have:
a=square (b++);
This expands to:
a=b++*b++;
This is again undefined behavior. In your case, it appears that you're getting 4*3 (or 3*4), but again, you can't count on this behavior. The final value of b is 5 since it is incremented twice, but this too is undefined behavior.
In addition to Tom's answer, which explains what is happening, here is an example of how you could define a macro for squaring a number safely:
#define SQR(x) ( \
{ \
__auto_type x_ = (x); \
\
x_ * x_; \
} \
)
It only has an appearance of x, and therefore it doesn't evaluate it twice. The copy x_ is used instead. Note that variables created in a macro may conflict with other variables created in the function that calls the macro. To avoid name collisions you use special names that shouldn't be used in normal code such as a trailing _.
With this macro, this:
a = SQR(b++);
will be equivalent to this:
a = SQR(b);
b++;
Warning: This works on some compilers as an extension (GCC for example), but it is not standard C.
Another option, if you want standard C, is to use an inline function. It is ok if you want it to work on just one type (there is _Generic in C11, but I never used it, so no idea).
I want to use this macro to put (if 'i' is greater than zero) the symbol '^' and the number I pass (i) to the macro
#define ESP(i) ((i>0) ? ("^"(i)) : "")
I want to call it in this way
printf("%+d%s", n1, ESP(i));
where 'i' is the index of a cycle, but the compilation reports me errors;
how can I modify the code to be right?
Somewhat dirty but should work:
#include <stdio.h>
#define DYNFORMAT(n, i) (i>0) ?"%+d%s%d\n" :"%+d%s%s\n", n, (i>0) ?"^" :"", (i>0) ?i :""
int main(void)
{
int i = 0;
printf(DYNFORMAT(42, i));
i = 1;
printf(DYNFORMAT(42, i));
}
This should print:
+42
+42^1
Disclaimer: I am not sure whether this conforms to the Standard and how to get rid of the warning(s) it gives during compilation.
The clean approach would be to use two calls to printf().
This can be implemented as a macro or a function.
As I love the pre-processor, the macro version here:
#define PRINT_ESP(n, i) \
do { \
if (i = 0) \
printf("%+d", n); \
else \
printf("%+d^%d", n, i); \
} while (0);
Macros operate at compile time, not at run time. They can perform a variety of text-mangling tricks, but they do not evaluate anything. (They can certainly, however, expand to code that evaluates something.) Putting the formatted value of variable i into a string involves evaluating i; no macro can do this.
You could instead expand the scope of the macro to include the whole printf() call:
#define PRINT_ESP(n1, i) do { \
printf(((i > 0) ? "%+d^%d" : "%+d"), n1, i); \
} while (0)
Alternatively, you could use a macro to express just the format selection incorporated into the above macro definition, or you could just put the full printf() call above directly into your code.
All of these variations are based on the fact that arguments in excess of those required by the given format are evaluated prior to the call, but ignored by printf() itself.
You don't need the (i) after the "^". Change your macro to this and it should work #define ESP(i) ((i>0) ? ("^") : ("")). Then in your printf statement if you want to print the value of i after the "^" then have something like this printf("%s%d", ESP(i), i); As far as I know I don't think you can format a string to include an integer inside a macro since that would require calling other functions, so you have to bring the i into the string inside your printf.
How to retrieve the text expanded from a macro?
#include <stdio.h>
#define paste(front, back) front ## back
int main()
{
int number1 = 23;
int number2 = 64;
printf("%d\n", paste(number, 2));
return 0;
}
The output is: 64 because the macro is expanded as:
printf("%d\n", number2);
How can I print the identifier number2 as string by using the defined paste macro. I need to know how to create the output:
number2: 64 by not writing directly "number2" I don't want this solution:
printf("number2: %d\n", paste(number, 2))
I want a dynamic solution. I try to concatenate by doing:
printf("%s: %d\n", paste(number, 2), paste(number, 2));
But it doesn't work because number2 returned by the paste macro is an the identifier an integer How can I retrieve as a text (string)?
Use the stringizing directive #:
#define stringize_impl(x) #x
#define stringize(x) stringize_impl(x)
printf("%s: %d\n", stringize(paste(number, 2)), paste(number, 2));
On a side note: why are you trying to do this? Surely there must be a better solution.
If you're compiling with GCC you can call it with gcc -E filename.c to see the expansion of macros.
EDIT:
You can also use the stringize preprocessor operator # that effectively puts double-quotes around the right-hand symbol.
#include <stdio.h>
#define T(...) #__VA_ARGS__
#define paste(f, b) _paste(f, b)
#define _paste(front, back) front##back
int main()
{
int n = 5;
printf("macro expands to: '%s'\n", T(paste(number, 2), paste(number, 2)));
printf("macro expands to: '%s'\n", T(paste(n, 2)));
return 0;
}
This code hopefully answers the question.
You need to expand the macro one more time to expand the paste in the stringize. In other words, for the paste macro inside the stringize macro to expand, the preprocessor has to pass over the file one more time. That's why you pass it through another macro defined later in the file.
I'm not 100% sure of ALL the rules of the preprocessor, but this seems to hold pretty well. For every macro you want to expand inside another macro, you need to do some magic to force the preprocessor to pass over the file again :) There exist different ways of achieving this to my knowledge, but this is one.
EDIT2:
Edited the code. I am getting this output, is this what you want?
morten#laptop:/tmp$ ./a.out
macro expands to: 'paste(number, 2), paste(number, 2)'
macro expands to: 'paste(n, 2)'
my code is:-
#include<stdio.h>
#include<conio.h>
#define sq(x) x*x*x
void main()
{
printf("Cube is : %d.",sq(6+5));
getch();
}
The output is:-
Cube is : 71.
now please help me out that why the output is 71 and not 1331...
thank you in advance.
Always shield your macro arguments with parenthesis:
#define sq(x) ((x) * (x) * (x))
Consider the evaluation without the parenthesis:
6 + 5 * 6 + 5 * 6 + 5
And recall that * has a higher precedence than +, so this is:
6 + 30 + 30 + 5 = 71;
Get to know the precedence rules if you don't already: http://en.cppreference.com/w/cpp/language/operator_precedence
You need parentheses around the argument.
#define sq(x) ((x)*(x)*(x))
Without the parentheses, the expression will expand to:
6+5*6+5*6+5
Which you can see why it would evaluate to 71.
A safer solution would be to use an inline function instead. But, you would need to define a different one for each type. It might also be more clear to rename the macro.
static inline int cube_int (int x) { return x*x*x; }
If you define the macro like this:
#define sq(x) x*x*x
And call it:
sq(6+5);
The pre-processor will generate this code:
6+5*6+5*6+5
Which is, due to operator precedence, equivalent to:
6+(5*6)+(5*6)+5
That's why, the macro arguments must be parenthesized:
#define sq(x) (x)*(x)*(x)
So that pre-processor output becomes:
(6+5)*(6+5)*(6+5)
However, if you pass some arguments with side-effects such as (i++):
sq(i++)
It will be expanded to:
(i++)*(i++)*(i++)
So, be careful, perhaps you need a function