C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 13)
13.
What will be the output of the program?
#include<stdio.h>
int i;
int fun1(int);
int fun2(int);

int main()
{
    extern int j;
    int i=3;
    fun1(i);
    printf("%d,", i);
    fun2(i);
    printf("%d", i);
    return 0;
}
int fun1(int j)
{
    printf("%d,", ++j);
    return 0;
}
int fun2(int i)
{
    printf("%d,", ++i);
    return 0;
}
int j=1;
3, 4, 4, 3
4, 3, 4, 3
3, 3, 4, 4
3, 4, 3, 4
Answer: Option
Explanation:

Step 1: int i; The variable i is declared as an global and integer type.

Step 2: int fun1(int); This prototype tells the compiler that the fun1() accepts the one integer parameter and returns the integer value.

Step 3: int fun2(int); This prototype tells the compiler that the fun2() accepts the one integer parameter and returns the integer value.

Step 4: extern int j; Inside the main function, the extern variable j is declared and defined in another source file.

Step 5: int i=3; The local variable i is defines as an integer type and initialized to 3.

Step 6: fun1(i); The fun1(i) increements the given value of variable i prints it. Here fun1(i) becomes fun1(3) hence it prints '4' then the control is given back to the main function.

Step 7: printf("%d,", i); It prints the value of local variable i. So, it prints '3'.

Step 8: fun2(i); The fun2(i) increements the given value of variable i prints it. Here fun2(i) becomes fun2(3) hence it prints '4' then the control is given back to the main function.

Step 9: printf("%d,", i); It prints the value of local variable i. So, it prints '3'.

Hence the output is "4 3 4 3".

Discussion:
18 comments Page 1 of 2.

Yabee said:   5 years ago
But, fun1 and fun2 should return No output as we are printing the output instead of returning it.

Shreya said:   5 years ago
@Yashwant.

As you said int j is global but value of i is given so how can we assume it to be the value of j? Neither we have any pointer associated. Please tell me.

Yamini said:   7 years ago
Here, the concept of the call by value is used and we should understand that than giving preference to local or global.

Sachin said:   8 years ago
Thanks for the given explanation.

Adi said:   8 years ago
What does it mean "control is given back to the main"?

Vamsi said:   9 years ago
@Yashwanth P,
According to you, the value of j is discarded but here int fun1(int j).
For int j how can we take the value from another variable i? Can you explain?

Yashwanth P said:   9 years ago
The Local variable will have higher priority than the global variable. So the value of j = 1 is discarded.

In the fun1 and fun2 the value of 3 is pre-incremented and then displayed but the value of "i" in main remains 3 because the value of i = 3 and its scope is local.

Kavi said:   10 years ago
Anyone can explain briefly?

Saran said:   10 years ago
What about j?

Sherine said:   10 years ago
What about the variable j?


Post your comments here:

Your comments will be displayed after verification.