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
Related
This question already has answers here:
Is accessing a global array outside its bound undefined behavior?
(8 answers)
Closed 8 months ago.
I am a Computer science student and I feel like I am missing something very simple. Could you please help me out ?
#include <stdio.h>
void do_stuff(int *c) {
static int a = 0;
int b = 0;
a+=3;
printf("%d %d\n", *(c+a), c[b]);
printf("%d %d\n", *(c+6), c[b]);
printf("%d %d\n", c[6], c[b]);
}
int main (void){
static int array[6] = {5,17,23,42,127,3};
do_stuff(array);
do_stuff(array);
do_stuff(array);
return 0;
}
This is the outcome of this code:
42 5
3 5
3 5
6 5
6 5
6 5
0 5
9 5
9 5
I don't get, why it is 6 5 for the second do_stuff(array).
I thought it would be 0 5 for every print of second and third do_stuff(array). Then I thought maybe It was something to do with static a and I tried it without a variable, so just with the number 6. But the answer was the same.
Could you please explain the reason for the outputs with the bold font?
Thank you for your help.
In an array with 6 elements, using index 6 will read the first position after the array, which is not 0. The read value depends on the underlying architecture and compiler implementation; depending if such memory position is mapped to your process or not, the OS may kill your application.
In your case, it looks like in memory you have the value of variable a stored just after the input array of do_stuff(), that's why printing c[6] basically prints the value of a.
Of course this is best described as undefined behavior and the source code is basically incorrect.
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.
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`.
This question already has answers here:
Why is this Undefined Behavior?
(3 answers)
Closed 7 years ago.
#include<stdio.h>
int main()
{
int x=2;
x=x++;
printf("%d",x);
return 0;
}
Output according to my logic :
2
Output on windows :
3
Output on Linux:
2
Why is windows giving 3 as output.
From what I understand x++ increments 2 to 3 but return back 2. So x should have 2.Is windows evaluating this anything different.
Similarly:
#include<stdio.h>
int main()
{
int x=2,y=4;
x=x++ + ++y;
printf("%d %d",x,y);
return 0;
}
Output according to me :
7 5
Output in windows :
8 5
Output on Linux :
7 5
Again the same situation.
Please help.....
x = x++ is undefined behavior so the two compilers are generating two different pieces of code.
Just x++ should suffice for your first piece of code.
Here is a question with your exact problem for the second piece of code
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.