+ 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; }

30th Sep 2019, 9:16 AM
Nandini
21 Respuestas
2nd Oct 2019, 6:03 AM
Programmer Raja
Programmer Raja - avatar
+ 6
The global i is modified from 10 to 9 by g()
2nd Oct 2019, 5:24 AM
Sonic
Sonic - avatar
+ 6
However it is the local i of main() that's output.
2nd Oct 2019, 5:27 AM
Sonic
Sonic - avatar
+ 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().
30th Sep 2019, 9:29 AM
Ipang
+ 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.
30th Sep 2019, 9:59 AM
Ipang
+ 4
Ipang yeah may be the answer in quiz was wrong
30th Sep 2019, 10:32 AM
Nandini
+ 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.
2nd Oct 2019, 7:35 AM
Aravind Devara
Aravind Devara - avatar
+ 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.
30th Sep 2019, 9:38 AM
Nandini
+ 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 ??
30th Sep 2019, 9:54 AM
Nandini
+ 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
30th Sep 2019, 10:04 AM
jtrh
jtrh - avatar
+ 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.
30th Sep 2019, 9:44 AM
Ipang
+ 2
Ipang ok i understood, thank u
30th Sep 2019, 10:07 AM
Nandini
+ 2
I think (studio.h)should be (iostream) sorry if this is wrong because Iam a new coder
2nd Oct 2019, 5:56 AM
Tushit Sharma 24 CODER
Tushit Sharma 24 CODER - avatar
+ 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 😅
2nd Oct 2019, 7:11 AM
Sacchit Malhotra
Sacchit Malhotra - avatar
+ 2
Aravind Devara No actually the answer is 1 but in challenge the answer given as 9
2nd Oct 2019, 7:38 AM
Nandini
+ 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 : )
30th Sep 2019, 10:16 AM
Ipang
+ 1
I understood!!!
1st Oct 2019, 12:02 PM
I'm Rishi :)
+ 1
Can I see that challenge? Can I have the link Nandini how can it be 9!
2nd Oct 2019, 7:44 AM
Aravind Devara
Aravind Devara - avatar
0
The answer is 1 not 9 There is no return in function f and g So the output will be 1
7th Dec 2019, 6:34 AM
Kaviyarasan R
0
Actually answer is 1
18th Dec 2019, 7:29 AM
Sai sandesh Reddy
Sai sandesh Reddy - avatar