This question already has answers here:
The need for parentheses in macros in C [duplicate]
(8 answers)
Closed 2 years ago.
My code:
#include <stdio.h>
#define PRODUCT(x) (x * x)
int main()
{
int i = 3, j, k, l;
j = PRODUCT(i + 1);
k = PRODUCT(i++);
l = PRODUCT(++i);
printf("%d %d %d %d", i, j, k, l);
return 0;
}
I am not able to comprehend why the output is:
7 7 12 49.
Is there any error in macro or some other problem?
Your code has undefined behavior, operations in i:
k=PRODUCT(i++);
l=PRODUCT(++i);
lack a sequence point.
As for:
j=PRODUCT(i+1);
It expands to i+1*i+1 which is i+i+1 which is 7. I assume it's not the expected result, in the future also include that in your question.
Your macro is incorrect. The following expression:
PRODUCT(i+1)
will expand to
(i+1 * i+1)
which is 2*i+1.
Your macro should be:
#define PRODUCT(x) ((x)*(x))
I strongly suggest you stop using macros for this sort of thing. You could easily write this as a function:
int product(int x)
{
return x * x;
}
Note that this will only work for the example I gave. If you try
PRODUCT(i++)
you will get
( (i++) * (i++) )
which invokes undefined behaviour, as this expression lacks a sequence point between the 2 increments.
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:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 6 years ago.
First of all, I would like to apologize, if this question has been asked before in this forum. I searched, but could't find any similar problem.
I am a beginner in C. I was going through a tutorial and came across a code, the solution of which I can't understand.
Here is the code -
#include <stdio.h>
#define PRODUCT(x) (x*x)
int main()
{
int i=3, j, k;
j = PRODUCT(i++);
k = PRODUCT(++i);
return 1;
}
I tried running the code through compiler and got the solution as "j = 12" and "k = 49".
I know how #define works. It replace every occurrence of PRODUCT(x) by (x*x), but what I can't grasp is how j and k got the values 12 and 49, respectively.
Any help would be appreciated.
Thank you for your time.
Your code will invoke undefined behavior. Anything could happen. The macro in statements
j = PRODUCT(i++);
k = PRODUCT(++i);
will be expanded to
j = x++ * x++;
k = ++x * ++x;
In both statements x is being modified more than once between two sequence points.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
I wrote a small piece of code in which I used #define with increment operator. The code is
#include <stdio.h>
#define square(a) ((a)*(a))
int main ()
{
int num , res ;
scanf ("%d",&num ) ;
res = square ( num++ ) ;
printf ( "The result is %d.\n", res ) ;
return 0 ;
}
But while compiling this in gcc, I got the following note and warning:
defineTest.c:8:20: warning: multiple unsequenced modifications to 'num' [-Wunsequenced]
res = square ( num++ ) ;
^~
defineTest.c:2:21: note: expanded from macro 'square'
#define square(a) ((a)*(a))
^
1 warning generated.
Please explain the warning and note.
The output I got was :
$ ./a.out
1
The result is 2.
$ ./a.out
2
The result is 6.
Also explain how the code works.
Your macro, after preprocessor expands it, will look like this:
((a++)*(a++))
Notice that, a++ is the same as a = a + 1, so you can rewrite expanded macro as:
((a = a + 1)*(a = a + 1))
You are changing value of a (or num, to be exact) twice in one statement, which generates warning, as this is undefined behaviour.
I would suggest you to rewrite your macro as a function
int square(int x)
{
return x*x;
}
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
For the testing code as follow:
#include <stdio.h>
int addTen(int x, int b[])
{
b[2] = x + b[2];
return b[2];
}
void main(void)
{
int a[3] = {4,5,6};
int i = 2;
printf("%i %i %i \n", a[i], addTen(10,a), a[i]);
}
Why is the output is 16, 16, 6? I know that even if the compiler processes the order from right to left like a[i] <- addTen(10,a) <-a[i]. After calling addTen(10,a), a[i] is already 16 (not 6). So why the output is not 16, 16,16? THANKS!
It's undefined behavior, you should read about sequence points. You're modifying a and simultaneously reading it in a same expression.
In addition the order of evaluating is not defined.
There's no order defined for evaluating arguments. The compiler is free to evaluate arguments in any order, and will normally choose the most convenient order. So, you can't define any expected output.
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=