please help to explain the output of this c program [duplicate] - c

This question already has answers here:
Why the following function is called thrice
(6 answers)
Closed 5 years ago.
After executing i am getting the output as 12 6 11. please explain how this is possible
#include<stdio.h>
#define MAN(x,y) (x)>(y)?(x):(y)
int main()
{
int i = 10,j = 5,k = 0;
k = MAN(i++,++j);
printf("%d %d %d", i, j, k);
return 0;
}

The macro will expand the line
k=MAN(i++,j++)
as
k=(i++)>(j++)?(i++):(j++);
k=(i++)>(j++)?(i++), (j++) will not be evaluated.
so
i will be incremented twice and result in 12 j will be incremented once so 6 when k will be assigned i value is 11 so its value as11`.

Related

Arrays: accessing array elements by inverting the index variable with the array itself yields the same result [duplicate]

This question already has answers here:
Different way of accessing array elements in C
(8 answers)
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 4 years ago.
So the code looks like this (you can access this fiddle here (Click me):
#include <stdio.h>
int main()
{
int a[5] = { 6, 2, 7, 3, 5 };
for (int i = 0; i < 5; i++){
printf("%d ", i[a]);
}
printf("\n");
for (int i = 0; i < 5; i++){
printf("%d ", a[i]);
}
return 0;
}
And this is the output:
6 2 7 3 5
6 2 7 3 5
Apparently, he made a mistake when indexing the array, and inverted the index variable with the array itself. What he found out is that it still prints the same values.
This might be a silly question, but again, I don't know much about these types of cases, and this itches me to find out why it's happening what's happening here.
Thanks
x[y] is equivalent to *(x + y) which is equivalent to *(y + x) which is equivalent to y[x].

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.

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.

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

Uncertain output of this C code [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
Why does sizeof(x++) not increment x?
Here the value of i is 5 but it should be 6 or 7 I guess. What's the reason?
int main(void){
int i=5,j;
j=sizeof(i++ + ++i);
printf("%d %d",i,j);
}

Resources