C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 8)
8.
What will be the output of the program?
#include<stdio.h>

int main()
{
    int fun(int);
    int i = fun(10);
    printf("%d\n", --i);
    return 0;
}
int fun(int i)
{
   return (i++);
}
9
10
11
8
Answer: Option
Explanation:

Step 1: int fun(int); Here we declare the prototype of the function fun().

Step 2: int i = fun(10); The variable i is declared as an integer type and the result of the fun(10) will be stored in the variable i.

Step 3: int fun(int i){ return (i++); } Inside the fun() we are returning a value return(i++). It returns 10. because i++ is the post-increement operator.

Step 4: Then the control back to the main function and the value 10 is assigned to variable i.

Step 5: printf("%d\n", --i); Here --i denoted pre-increement. Hence it prints the value 9.

Discussion:
12 comments Page 1 of 2.

Saiteja said:   1 decade ago
IN the above program, whenever the first function gets called I++ will be returned but the value is not there for i.

When the second function, gets called i.e., int i = fun(10);

i has its value 10 it may be incremented even though its post increment ,but according to CALL BY VALUE even though the value of i is incremented in the int fun(int i) the value of 'i' will not be modified in the main hence PRINTF will print '9' but not 10 .
(1)

Pihu said:   1 decade ago
It happens all because of post increment operator,the value of i++ is incremented after return statement because post increment works with firstly assign then increment.so i++ value will be 10 when it goes to the main function that is the case and when i is being assigned to value in main it will be 10 and after printf 9 will be printed because of pre decrement which works first by decrementing the value and then assign it.
(2)

Bagesh kumar singh said:   1 decade ago
int fun(int i)
{
return(i++)
}
it means first return the values after that it increment the value of the i. and in main in prentf statement it derecrement the value of i.here i=9 becuse the value of i i pre-increment .

in place
int fun(int i)
{
return(++i);
}
so that output is i=10;
becuse here pre increment .it will return the i=11 and in main it will the dercement the value of i.

so the the value of i=10;;;;

Rupinderjit said:   1 decade ago
But we have called function here with value 10(call by value), so whatever the operation may perform in definition will not appear in called function. Isn't it?

Abi said:   1 decade ago
Here we are using brackets in function, that means the return value be 11 and the output value be 10.

Priya said:   9 years ago
After post increment, the i value is 11 but in program i value is 10. Then last i value is 10.

Karthi said:   1 decade ago
return (10++) means it return 10 after it increment. Near we no need what happen after words.

Karan said:   1 decade ago
Declaration should be above the main. So please can anybody tell me about this?

Yasmin said:   8 years ago
What will be the o/p? If we use post decrement operator in main function?

Priya said:   1 decade ago
@bagesh:we get 9 when executed in turbo c++.what is the reason?


Post your comments here:

Your comments will be displayed after verification.