Pre/post increment with #define in c [duplicate] - c

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;
}

Related

How does it works? Sequence of computing variable into printf [duplicate]

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.

Unexpected output in expression that uses macros [duplicate]

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.

Strange behavior of #define [duplicate]

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);

gcc compiler printf execution order [duplicate]

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.

Unary operation are sometimes suicidal.Mess up with C code [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
Code in C language.
#include<stdio.h>
#define PRODUCT(x) ( x * x * x)
int main()
{
int i =5,k;
k = PRODUCT( ++i );
printf("i is :%d ",k);
return 0;
}
My Question is why i is : 392? According to me output should be 336. (because 6 * 7 * 8 = 336)
Am I really messed up here??
Preprocessed code will have
( ++i * ++i * ++i)
which have Lack of sequence point between the two execution on same variable resulting Undefined behaviour.

Resources