+ 2

what is output of given code void func(int x) { x+=6; } int main() { static int x; func(x); cout<<x; return 0; } // zero output explanation given below

14th Jun 2016, 11:05 AM
Dev!L
Dev!L - avatar
10 Antworten
+ 5
it is 0 not because both variables are named x but because the copy of the value is being passed to the function. The original value of static int x is 0 after calling the function. If u want to change its original value pass it by reference put void func (int& x)
14th Jun 2016, 5:30 PM
Sardor
Sardor - avatar
+ 2
The Code Playground in SoloLearn outputs 0.
14th Jun 2016, 12:22 PM
James Flanders
+ 2
address of both variable named x are different and if we r declearing any variable using static key word it automatically initialized as 0 so value of x in main function is zero so the output is zero
14th Jun 2016, 2:56 PM
Dev!L
Dev!L - avatar
+ 1
maybe it will be something like "undefined behaviour"
14th Jun 2016, 11:38 AM
Igor Lihachev
Igor Lihachev - avatar
+ 1
nothing as it is not returning any value to main
14th Jun 2016, 1:54 PM
VARUN KUMAR
VARUN KUMAR - avatar
+ 1
you're returning 0; all the other code is irrelevant in this context.
21st Jun 2016, 9:45 PM
Steven Trippier
Steven Trippier - avatar
0
m not sure but if value of x is 0 then output is 6 (correct me programmers of m @fault) explain please
15th Jun 2016, 3:35 AM
p@w@n
p@w@n - avatar
0
output 0
15th Oct 2018, 2:14 PM
karishma tejwani
karishma tejwani - avatar
0
Static keyword specifies the value assigned to the variable is remains unchanged with in the scope of block. Static variable's default value is 0. Hence output is 0
1st Dec 2018, 4:54 PM
Kondeti Sowjanya
Kondeti Sowjanya - avatar
- 1
correct ans is 0
14th Jun 2016, 2:41 PM
Dev!L
Dev!L - avatar