0
if we do not initialize variable it should print some garbage value but it printing 0 value by default...why this happening?
22 odpowiedzi
+ 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)
+ 2
He is talking about it
int c;
printf("%d",c);
It prints 0 and says c is uninitialised
+ 2
Well it prints c or what is 0 then?🤔
+ 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.
+ 1
Where is your code?
+ 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?
+ 1
ok
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.
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
0
here is my code...!
https://code.sololearn.com/cLL2GN0j9l5z/?ref=app
0
I also ran it on another compiler but it gives me same result...
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.
0
for local variable by default value is not zero but in some IDE it gives zero.
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.
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.
0
I think it depends on the compiler implementation. I remember that when using Turbo C, garbage value was assigned to uninitialized variables
0
Share your program, let's figure out ...
0
I got that point before ...!
0
for local variable there's no any default value .. and that '0' is also a garbage value