0

Why my answer is different in two condition How can anyone explain me?

Below there are two codes of same program but some minor different at static variable declaring In 1st part of program I have declared x variable value in same line so output show 56 But, I second part of program i have defined x in first line and assign value in second line so why output is 55 the technique is same as logically. FIRST PART OF PROGRAM👇 https://code.sololearn.com/cop3vwG3kUea/?ref=app SECOND PART OF SAME PROGRAM👇 https://code.sololearn.com/cMBA4iVuLwi0/?ref=app Help me to understand it.

28th Jan 2020, 3:44 PM
PRINCE KUMAR
4 Réponses
+ 2
1st code output 56. You declare and initialize the static variable in a single line so whenever you make a call to the test() it will result in an updated value. 2nd code output 55. You declare the static variable first and then assign the value in a new line. So whenever the test method is called, the static is assigned with the same value 5 even though you do an x++ because it is canceled out by x=5 in the beginning of the test().
28th Jan 2020, 4:10 PM
Avinesh
Avinesh - avatar
+ 1
Additionally to Avinesh, it is recommended for you to move the `Test` function out of the `main` function. Not all compilers are happy seeing function inside function.
28th Jan 2020, 4:17 PM
Ipang
0
Avinesh if we perform this code in simple way without using method the result is different and also why it cancels x++. can you elaborate it or provide any reference to understand
28th Jan 2020, 5:13 PM
PRINCE KUMAR
0
You shouldn't have taken the literal meaning. I was trying to say that x++ will have no effect in the second code because the function assign x to 5, every time test() is called.
1st Feb 2020, 7:32 AM
Avinesh
Avinesh - avatar