+ 11
Can someone explain how the answer is 9
#include <stdio.h> int i=10; void f(void){ int i=5; i=7; } void g() { i=9; } int main() { int i=1; g(); f(); printf ("%d",i); return 0; }
21 Respuestas
+ 8
The answer is one not nine
https://code.sololearn.com/c8iKSET1FY84/?ref=app
+ 6
The global i is modified from 10 to 9 by g()
+ 6
However it is the local i of main() that's output.
+ 5
Unless you comment out the line `int i=1;` the output will be 1 not 9. The reason, declaration of <i> as local variable in main shadows the global variable <i>, and when you print <i> the local variable <i> having value 1 is used instead of the global <i> whose value is changed in function g().
+ 4
No Nandini , f() declares its own <i> variable locally, and also changes the local <i> value from 5 to 7. But again if we comment out the local variable declaration `int i = 5;` then the same scenario happens, the global <i> variable will be changed in f() also.
+ 4
Ipang yeah may be the answer in quiz was wrong
+ 4
Do u really got the output 9?
it will print 1 not nine, because in general if you are doing some operation on variable, c language will check whether it is present in the local method if it not then only it will go for global variables. one point to note is scope of a local variable is with in that method only.
+ 3
The printf statement in main function should points to i=1 but how function g() changed its value as it is not pass by reference.
+ 3
Ipang as u r saying that if we comment i=1 then output is 9
After calling function g(),we are calling f().. Wont f() change the value of i ??
+ 3
To quote from wikipedia:
"Local variable references in the function or block in which it is declared override the same variable name in the larger scope."
https://en.wikipedia.org/wiki/Local_variable
+ 2
You are right that printf() call should print the local variable <i> declared in main(). That is if we keep the declaration `int i = 1;`. If we comment that line, then the <i> variable recognized in g() and main() will be the one declared global (`int i = 10;`) on top of the code. This global variable is the one recognizable in both g() and main(). The g() function is the one who changed its value from 10 to 9.
+ 2
Ipang ok i understood, thank u
+ 2
I think (studio.h)should be (iostream) sorry if this is wrong because Iam a new coder
+ 2
The answer would be 1
As when the main runs tge variable i is created in its memory, when g() function is called the i in the memory g() is converted to 9 from junk value, when f() is called the i in the memory of f() is declared to 5 and then changed to 7 but none of the functions returns the value.
The value of i in the main() remains tge same which is equal to 1 and 1 is printed as the output.
If you understood my answer then give me a follow back, I'm new here 😅
+ 2
Aravind Devara No actually the answer is 1 but in challenge the answer given as 9
+ 1
Nandini
You're most welcome.
But the code you given above (as it is) will output 1 instead of 9, unless we comment the declaration of local variable <i> in main(). If that is exactly how the code is looking about in the challenge, then I suspect the quiz is a faulty one : )
+ 1
I understood!!!
+ 1
Can I see that challenge? Can I have the link Nandini how can it be 9!
0
The answer is 1 not 9
There is no return in function f and g
So the output will be 1
0
Actually answer is 1