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 2 of 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;;;;

Seetha said:   2 decades ago
I have doubt in this. Please send detail about this concept.


Post your comments here:

Your comments will be displayed after verification.