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);
Related
This question already has answers here:
#define used with operators [duplicate]
(4 answers)
Closed 3 years ago.
I ran it through Code::Blocks and it shows me the final answer 1. How is TWO then replaced in the " i = i-2*TWO" statement and why is like that?
The code is part of a homework exercise I'm trying to solve/understand:
#include <stdio.h>
#define ONE 1
#define TWO ONE + ONE
int main(void) {
int i = 2;
i = i - 2 * TWO;
printf("%d\n", i);
return 0;
}
Just make the substitution yourself.
i = i - 2 * TWO;
is
i = i - 2 * ONE + ONE;
So you have
i = 2 - 2 * 1 + 1;
So i will have the value 1.
If you want to get the result equal to -2 then rewrite the macro like
#define TWO ( ONE + ONE )
You can see what the compiler sees as final expression by running gcc -E <filename.c>. That should help in understanding why the result is 1.
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 behavior of Macro-expansion
(3 answers)
Why is the return value not what I expected in this C program with macro? [duplicate]
(2 answers)
Closed 7 years ago.
#define A 1+2
#define B 4+3
int main()
{
int val = A*B;
printf("%d",val);
}
Here A and B is defined, and val is (A*B). What is the correct output?
Let's trace the expansion and calculation manually.
A*B -> 1+2*4+3 -> 1+8+3 -> 12
As a result, the output will be 12
This is a common issue with Macros. When you define something even as x+y in a macro it is advisable to first rap them in () so as to "protect" the operation.
As such it would be better to define A as (1+2) etc. Otherwise you get the output 1+2*4+3=12 as people have stated above.
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=