9th Jan 2021, 9:20 AM
Suresh Rajendiran
5 Respostas
+ 3
Your Code: #include <stdio.h> int fun(int a,int b){ printf("%d %d\n",a,b); if(b==0) return 0; if(b%2==0) return fun(++a + a++,b/2); return fun(++a + a++,b/2)+a; } int main() { int x=3,y=2; int z=fun(y,x); printf("%d",z); return 0; } 1. You have a function named fun which takes in 2 integer (a and b) parameters. 2. In the func, you first print the 2 integers. 3. If b is 0 then you return the function with nothing. 4. If b is an even number then, then you run the function fun again (which is known as recurrsion) by passing 1st parameter as (a+1)+a and 2nd parameter as b/2. 5. As you already returned from the function the next return will not be considered. 6. In the driver function or the main function, you are passing 3 and 2 to the function fun and getting the value in z. 7. Here, you are printing z. In this, program, whatever value you put in x and y in the main, you will get 1 in z. So, therefore it will print 1, in whatever case.
9th Jan 2021, 9:45 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
+ 2
See the warning about sequence point. You are accessing and modifying data in a sequence, there is no definite or correct in this case. You may see different outputs with different compiler.
9th Jan 2021, 9:47 AM
Ipang
+ 2
This is not correct one Ipang .. In your point answer the question based on this compiler... Kindly refer operator precedence rule.. I checked 4 compilers all compilers give same answer..
9th Jan 2021, 10:17 AM
Suresh Rajendiran
+ 2
Irrespective of any other compiler, output here is 2 3 7 1 17 0 13 the output 2,3,7,1,17,0 are understandable (if not ,you can ask me) but I am figuring out how 13 is the final value returned by the function .
9th Jan 2021, 11:02 AM
Abhay
Abhay - avatar
+ 1
Suresh Rajendiran Operator precedence hardly play a role where sequence point violation occurs. Notice that I wrote "you may see different outputs ...", I didn't say you *will*. I tested the code in C4Droid and it gave different output BTW. And about sequence point, and the triggers, kindly refer this https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points
9th Jan 2021, 11:18 AM
Ipang