0
Output question in c
int i=10; void f(void){ int i=5;i=7; } void g() { i=9; } int main() { I=1; g(); f(); printf("%d", I); return 0; } Why is the output 9 here?
6 Answers
+ 9
In the f() function the variable i is declared again, making it a local variable, specific to the f() function only. Any changes within f() will not apply to the global version of i.
Function g() however does not redeclare the variable i and therefore its changes apply to the global variable.
Thus g() causes i = 9. But the function f() changes are do not apply to i.
Hopefully that makes sense.
+ 2
Because i is originally declared as a global variable but is not const. Therefore i can be changed in any function, including those with a return type of void - just as long as it isnât declared again as a local variable as it is in f().
0
int i=10;
void f(void){
int i=5;i=7;
}
void g()
{
i=9;
}
int main()
{
i=1;
g();
f();
printf("%d", i);
return 0;
}
Why is the output 9 here?
0
But why not the answer should be 1
0
As other function are of void nature so changes remains unaffected
0
Ok thanks