C #define strange outputs [duplicate] - c

This question already has answers here:
The need for parentheses in macros in C [duplicate]
(8 answers)
Closed 5 years ago.
#include <stdio.h>
#define test(x) x*x
#define test2(x) x
int main(void) {
int x=5;
printf("%d %d %d %d %d %d",test2(x), test2(x-2), test(x), test(x-2),4*(test(x-3)),4*test(x-3));
return 0;
}
This gives the output as :
5 3 25 -7 -52 2
Well can understand for first 3 but why -7 when test(x-2) and for last 2…

After compiler preprocessing step your printf becomes
printf("%d %d %d %d %d %d",x, x-2, x*x, x-2*x-2,4*(x-3*x-3),4*x-3*x-3);
x-2*x-2 is evaluated as:
x-2*x-2 -> x-(2*x)-2
5-10-2
= -7
ie * takes precedence over -

Your this expression will expand something like given at the second code snippet: Below code is indented just to easily show you the code, I know it is syntactically wrong. :)
printf("%d %d %d %d %d %d",test2(x),
test2(x-2),
test(x),
test(x-2),
4*(test(x-3)),
4*test(x-3));
After expansion:
printf("%d %d %d %d %d %d",x,
x-2,
x*x,
x-2*x-2,
4*(x-3*x-3),
4*x-3*x-3);
Macros doesn't replace expression with brackets enabled, rather it does the literal replacement.
If you do the maths, you will get the answer which you had already shown on the top.

Related

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.

C arguments from the C primer plus 6e should effect print [duplicate]

This question already has answers here:
What can happen if printf is called with a wrong format string?
(5 answers)
Closed 4 years ago.
C written in Emacs
// print1.c --displays some properties of printf()
#include <stdio.h>
int main(void)
{
int ten = 10;
int two = 2;
printf("Doing it right: ");
printf("%d minus %d is %d\n", ten, 2, ten - two);
printf("Doing it wrong: ");
printf("%d minus %d is %d\n", ten); // forgot 2 arguments
return 0;
}
Welcome to the Emacs shell
Output:
~ $ ./a.exe
Doing it right: 10 minus 2 is 8
Doing it wrong: 10 minus 2 is 8
It should do nothing of the sort.
The behaviour of your program is undefined.
One manifestation of undefined behaviour is the compiler figuring out what you really wanted to do. Don't ever rely on that though.

Format specifier in c [duplicate]

This question already has answers here:
What does "%.*s" mean in printf?
(4 answers)
Closed 6 years ago.
Why the following program work ? & what is difference b/w %d vs %*d ?
#include<stdio.h>
int main()
{
int n=5;
printf("n=%*d\n", n, n);
return 0;
}
What does width meant here by ?
The first n belongs to the * in %*d, so for your example it is %5d. %5d means to print an integer of width 5. Example: 234 is printed as __234 (2 spaces)

variable length argument in c program [duplicate]

This question already has answers here:
Order of operations for pre-increment and post-increment in a function argument? [duplicate]
(4 answers)
Closed 7 years ago.
In the following program
#include<stdio.h>
int main()
{
int a;
a=5; printf("%d %d %d\n", a, a++ , a++); //statement 1
a=5; printf("%d %d %d\n", a, ++a , ++a); //statement 2
return 0;
}
Output
7 6 5
7 7 7
My question why there is different behavior with a++ and ++a. I know in variable length argument it is executed from left to right and statement 1 make sense but I am wondering the result for statement2 and I was expecting results like 7 7 6..Am I missing something here ?
The order in which arguments to a function are evaluated is unspecified. It's your responsibility to write code that works the same regardless of this order.

output of a function which has passByName parameter passing paradigm [duplicate]

This question already has an answer here:
Output of a c code like with call by reference [closed]
(1 answer)
Closed 9 years ago.
I've tried to figure out what is the output of a code like this .By the way ,It is not a real question, kind of therical question, i mean it is not an original c code, it is kind of a PL having c-code syntax and passed by name parameter paradigm.
int x=12,y=10;
void tswap(int pa, int pb) {
int tmp;
tmp=pa;
pa=pb;
pb=tmp;
x=x+pa;
x=x-pb;
y++;
printf("%d %d %d %d\n",pa,pb,x,y);
}
int main() {
int a=4;
tswap(x,a);
printf("%d %d %d\n",x,y,a);
tswap(++x,++y);
printf("%d %d %d\n",x,y,a);
return 0;
}
I think the output of first part should be :
-4 12 -4 11
-4 11 12
But i could find a logical solution for the part tswap (++x, ++y)
Is there anyone who can know how can I handle with this part ?
Thanks in advance !
tswap(++x,++y)
is the same as:
++x;
++y;
tswap(x,y);
making your output:
4 12 4 11
4 11 4
12 5 12 13
12 13 4

Resources