This question already has an answer here:
C preprocessor Literal Construction
(1 answer)
Closed 6 years ago.
I have
#define STRING "string"
I want to add L prefix to STRING macro later in my code (I can't do it in definition). How to do it?
This should do:
#include <stdio.h>
#define CONCATENATE(e1, e2) e1 ## e2
#define PREFIX_L(s) CONCATENATE(L, s)
#define STRING "string"
int main(void)
{
printf("%ls\n", PREFIX_L(STRING));
}
These standard C pre-processor's concatenation capabilities are also covered by the documentation coming with GCC: https://gcc.gnu.org/onlinedocs/cpp/Concatenation.html
This should work.
#define STRING(x) x"string"
char * l_pString = STRING("");
wchar_t * l_pWideString = STRING(L);
Related
This question already has answers here:
C macros and use of arguments in parentheses
(2 answers)
Closed 3 years ago.
I am trying to write this code in code blocks C editor but the output is coming 36 instead of 1.
#include <stdio.h>
#define square(x) x*x
int main()
{
int p=36/square(6);
printf("%d",p);
}
Why is the output 36 instead of 1?
The problem is that the definition of your macro doesn't have any parentheses in it - and macros aren't 'called', like functions are - they just cause text to be replaced.
So, in your code, the line:
int p=36/square(6);
is replaced with:
int p=36/6*6;
which is evaluated in left-to-right order (as the * and / operators have equal precedence), giving, effectively:
int p = (36/6) * 6;
To fix this, add parentheses in your macro definition:
#define square(x) ((x)*(x))
Feel free to ask for further clarification and/or explanation.
The C preprocessor changes the line
int p=36/square(6);
into
int p=36/6*6;
You will get the desired output if your change your preprocesor macro to:
#define square(x) (x*x)
That way, the line will instead be changed to:
int p=36/(6*6);
However, this solution still is not ideal: If you for example write
int p=36/square(6+3);
then the preprocessor will change this to
int p=36/(6+3*6+3);
Therefore, the proper solution would be to change the preprocessor macro to:
#define square(x) ((x)*(x))
That way, the preprocessor will correctly change it to:
int p=36/((6+3)*(6+3));
In this respect, C preprocessor macros behaves very differently from function calls, where the parentheses are not necessary.
This question already has answers here:
Syntax error while using a #define in initializing an array, and as arguments to a function in C? [closed]
(2 answers)
Closed 5 years ago.
I've tried to figure out this behavior of the define command in C (I'm new with that). I've got this code and I don't know why I see in the output that myAge=15 and not 16 (I know it's 15, but I don't know why). Anybody can help me to find out why does it happen?
this is the code:
#include <stdio.h>
#include <stdlib.h>
#define AGE 15;
int main(void)
{
float myAge = AGE + 1;
printf("Hello!\n");
printf("My name is Raz and I am %d years old!\n", myAge);
system("PAUSE");
return 0;
}
Thanks for helping :)
#define is a textual replacement performed by the preprocessor prior to the compilation step. In this case, you're asking the preprocessor to expand the token AGE to 15;. The semicolon is part of the expansion, so this is the code you would get after the preprocessing step:
float myAge = 15; + 1;
As you can see, it does not expand to what you expect.
You can fix this issue by removing the semicolon from the #define:
#define AGE 15
Better yet, avoid using the preprocessor for simple numerical constants - consider using a const int instead:
const int age = 15;
This question already has answers here:
Strange behaviour of macros C/C++
(5 answers)
Closed 8 years ago.
I have the following piece of code.
#include <stdio.h>
#define d 10+10
int main()
{
printf("%d",d*d);
return 0;
}
As 10+10=20, I thought that d would be 20 everywhere in the program. But when I execute d*d, I have expected the result to be d*d=20*20=400. But the result gets printed as 120. Can anyone give me an explanation for this behavior?
Preprocessor is just doing simple "find & replace", so this code:
printf("%d",d*d);
changes to
printf("%d",10+10*10+10);
which is 10+100+10 = 120
That's why it's so important to add parens in defines:
#define d (10+10)
printf("%d",d*d);
Here d is replaced as printf("%d",10+10*10+10). so in this case it first executes 10*10 and adds with 10+100+10. so results 120.
To eliminate this-
#define d (10+10) // FIX
int main()
{
printf("%d",d*d);
return 0;
}
Change
#define d 10+10
to
#define d (10+10)
What you get without the proper parentheses is 10+10*10+10 which is the same as 10 + (10 * 10) + 10
printf("%d",d*d) will evaluate (after preprocessing) to printf("%d", 10+10*10+10);
This question already has answers here:
Strange behaviour of macros C/C++
(5 answers)
Strange behavior of Macro-expansion
(3 answers)
Closed 9 years ago.
When I run the following code, the return value is 11, but I was expecting it to return 25. Can someone explain this?
#include<stdio.h>
#define SQR(a) a*a
int main()
{
int i=3;
printf("%d",SQR(i+2));
return 1;
}
Needs more parentheses. This:
#define SQR(a) a*a
expands to this:
i+2*i+2
which is:
3+2*3+2
which is 11 because * has precedence over +.
You need to define your macro like this:
#define SQR(a) ((a)*(a))
to ensure that this kind of thing doesn't happen.
Macros are not the same as regular functions.
During the reprocessing all macros are replaced exactly by what they are define. In your case, the line:
printf("%d",SQR(i+2));
is replaced by the line:
printf("%d", i+2*i+2);
So, you see the unexpected result there.
The correct way is:
#define SQR(a) ((a)*(a))
The preprocessor result will be:
printf("%d", ((i+2)*(i+2)));
Try to learn on this mistake. Issues of this kind are quite hard to debug.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Macro definition error in C?
I'm new to programming and hope someone can help me with this:
Why is it giving an output : 5 .
Here is the code snippet:
#include <stdio.h>
#define max 5;
int main(){
int i=0;
i = max+1;
printf("\n%d",i);
return 0;
}
Because the macro has a semi-colon. Code is equivalent to:
i = 5; + 1;
Remove the semi-colon from the macro.
The crucial point is
#define max 5;
When substituted by the preprocessor, this will become
i = 5; +1;
which assigns 5 to the variable i (the expression +1; has no effect).
You need to write
#define max 5
Because you have a semicolon after your macro definition x=