This question already has answers here:
What is argument evaluation?
(2 answers)
Closed 6 years ago.
Problems using define in C. Works well when I call OP(9), but when I call OP(7+2) I get 23. Why?
#include<stdio.h>
#include<stdlib.h>
#define OP(x) x*x;
int main() {
int x,y;
x = 2;
y = OP(7+2);
printf("%d", y);
return 0;
}
Why prints 23 and not 81?
You should wrap x in parentheses in order to force precedence. However, it is also essential to wrap the entire expression in parentheses.
#define OP(x) ((x)*(x))
Related
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 2 years ago.
stumbled upon such a puzzle:
What will be shown on the screen?
#include <stdio.h>
void main()
{
int x = 10;
printf("x = %d, y = %d", x--, x++);
}
Curiously enough, but shown at the screen this: x = 11, y = 10;
But how??
Argument Evaluation Order is undefined in both C and C++. It's important to avoid any code that passes expressions dependent on each other that has to be evaluated before the function is called. It's a strict no.
int f1() { printf("F1") ; return 1;}
int f2() { printf("F2" ) ; return 1;}
printf("%d%d", f1(), f2()) ;
You can check out by adding several functions that contain a print statement and pass it to a function to observe this in action. You don't know what's coming, the C standard doesn't specify it, it depends on what compiler you use and how it optimizes your code.
This question already has answers here:
Invoke function instead of macro in C
(3 answers)
Why surround the function with parentheses?
(1 answer)
What do the parentheses around a function name mean?
(3 answers)
Closed 4 years ago.
I stumbled over a weird piece of code that I thought would not work the way it is intended, but it did. Example:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("%d\n", abs(-1)); // output: 1
printf("%d\n", (abs)(-1)); // output: 1
return 0;
}
Apparently putting parenthesis around the function name in a function call has no effect on the program. It just makes it look like a cast.
I am kinda interested if this is defined somehow. But I figure that it just not forbidden and that's why it works.
What I am really curious about is, is there a case where this kind of notation might yield an advantage in any kind? (structure of the code, readability, anything this might be useful for)
Or is it just a "weird" way to write code?
Try
int foo(int a, int b) { return a+b; }
#define foo(a,b) ((a)+(b)+1)
#include <stdio.h>
int main() {
printf("%d %d\n",
foo(3, 5), // Use macro if defined
(foo)(3, 5) // Always call function even if there's a macro
);
return 0;
}
Output:
9 8
See the difference?
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:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
When I compile the following code
#include<stdio.h>
#define CUBE(x) (x*x*x)
int main()
{
int a, b=3;
a = CUBE(b++);
printf("%d, %d\n", a, b);
return 0;
}
It gives 27 , 6
But shouldn't the expression a=b++*b++*b++; be calculated as a=3*4*5 and should give 60?
Your expression causes undefined behaviour, so you could get any answer. Trying to modify the same value more than twice between sequence points is bad news.
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=