0

if we do not initialize variable it should print some garbage value but it printing 0 value by default...why this happening?

31st May 2020, 4:37 PM
Shrinivas Gadade
Shrinivas Gadade - avatar
22 Respostas
+ 2
Did you declare a global variable or did you declare it as static?.....both cases would make the un-initialised var to default to 0. (or maybe you were just lucky)
31st May 2020, 4:41 PM
rodwynnejones
rodwynnejones - avatar
+ 2
He is talking about it int c; printf("%d",c); It prints 0 and says c is uninitialised
31st May 2020, 4:44 PM
Abhay
Abhay - avatar
+ 2
Well it prints c or what is 0 then?šŸ¤”
31st May 2020, 4:52 PM
Abhay
Abhay - avatar
+ 2
The C standard allows compiler developers to provide variable initialization if they wish. However, if your code depends on it, then your code will be less portable. Anyway, that does not appear to be the case here. If I use float c then I get some non-zero output. Just consider that 0 is also a valid garbage value. It just happened to be what it found in the memory at the time.
31st May 2020, 5:17 PM
Brian
Brian - avatar
+ 1
Where is your code?
31st May 2020, 4:42 PM
Maninder $ingh
Maninder $ingh - avatar
+ 1
I observe that same consistent result, and that leads me to speculate SoloLearn might execute the code in a virtual environment that gets re-created the same way for every run. Can anyone confirm it?
1st Jun 2020, 3:47 AM
Brian
Brian - avatar
+ 1
ok
1st Jun 2020, 4:18 AM
Shrinivas Gadade
Shrinivas Gadade - avatar
0
Abhay if you don't initialize c with 0 it will automatically pick garbage value and even give you a garbage value in the printf statement.
31st May 2020, 4:47 PM
Maninder $ingh
Maninder $ingh - avatar
0
ok, we consider 0 as garbage value but garbage value changes when we print same variable value number of times but gives me same result constantly... & code is #include<studio.h> int main() { int var; printf("%d", var); return 0; } ...& variable is local to the main function
1st Jun 2020, 1:09 AM
Shrinivas Gadade
Shrinivas Gadade - avatar
1st Jun 2020, 1:20 AM
Shrinivas Gadade
Shrinivas Gadade - avatar
0
I also ran it on another compiler but it gives me same result...
1st Jun 2020, 3:50 AM
Shrinivas Gadade
Shrinivas Gadade - avatar
0
Shrinivas Gadade this is solo learn feature as you can see that your code giving uninitialized error. actually, 0 you got on the output console is nothing this is just added by sololearn servers. Your code returns nothing. Try to run your code on PC on IDE something or android app coding c you will get the difference.
1st Jun 2020, 3:53 AM
Maninder $ingh
Maninder $ingh - avatar
0
for local variable by default value is not zero but in some IDE it gives zero.
1st Jun 2020, 7:19 AM
Shrinivas Gadade
Shrinivas Gadade - avatar
0
bcoz you haven't added your code in the question, i was assuming your variable as global. Just like in some IDE , in sololearn also the default value for local variable is zero.
1st Jun 2020, 7:41 AM
Amaan Khan
Amaan Khan - avatar
0
Look In C Language It Includes 4 Types Of Storage Classes : 1. auto 2. static 3. register 4. extern when youā€™re declaring your variable: int x; By Default It Takes ā€œautoā€ storage class. Whose initial value is garbage. but for extern and static variables which you have to declare by yourself, the initial value is 0. but for register storage class our intial value is garbage. May Be With This Explanation You Can Understand Much About Storage Classes.
2nd Jun 2020, 4:14 AM
Rahul Saxena
Rahul Saxena - avatar
0
I think it depends on the compiler implementation. I remember that when using Turbo C, garbage value was assigned to uninitialized variables
2nd Jun 2020, 7:50 AM
Lingesan S
Lingesan S - avatar
0
Share your program, let's figure out ...
2nd Jun 2020, 2:07 PM
RITESH SAH
RITESH SAH - avatar
3rd Jun 2020, 3:18 AM
Shrinivas Gadade
Shrinivas Gadade - avatar
0
I got that point before ...!
3rd Jun 2020, 3:31 AM
Shrinivas Gadade
Shrinivas Gadade - avatar
0
for local variable there's no any default value .. and that '0' is also a garbage value
3rd Jun 2020, 3:31 AM
Shrinivas Gadade
Shrinivas Gadade - avatar