+ 4
Int a=10; void main(){int a; printf(a);a=20; printf(a);}
what will be the output? I am confused accessing int a before initialisation will give garbage or default value of int? Edit:- The code is giving 0 value not garbage. Still confused i have two ans one is saying 1st print gives garbage value other is saying 1st print gives 0. https://code.sololearn.com/cBE96IV01aXu/?ref=app
25 Respuestas
+ 5
Jyoti Rani
To be on the safe side, I personally choose to initialize local variables. We shouldn't rely on different behaviour of compilers.
Yes the code runs, however you can also see that there's a warning for you because you used local variable <a> while it is not being initialized.
P.S. In other compilers you *may* expect different outputs.
+ 6
Uninitialized variables aren’t always zero in GCC either. A variable in C is just an address pointing to something (even if it’s not a “*” pointer). What it points to, if you don’t explicitly set it to something is mostly random. If the variable is in the static data section, it might be zero only because that particular runtime library sets all static data to zero. If it’s a local variable, meaning it’s on the stack, you get whatever use to be there, and chances are that’s actually a return address in the code somewhere. It’s a good programming habit to *always* explicitly initialize your variables. It’s an even better programming habit to use tools like lint to avoid finding these things in runtime.
In GCC, local variables are uninitialized. The value can be anything, including zero. Zero is a common value in local variables, so I would guess that it is just lucky coincidence that you saw an uninitialized var be equal to zero.
It won’t always be that way. GCC does not zero out local vars.
+ 3
Såñtösh Màràvi[Inactive] print is two times na one before initialisation and one after initialisation
+ 3
I think i got my answer. I run the code in turbo compiler and it is giving garbage value and searched in denis ritchie it also says garbage. Jayakrishna🇮🇳 Ipang you are right different compiler giver different value. I will go with standard book.
Thanks everyone for your help 🤗
+ 3
"Variable initialization" and "Variable assignment" are two different things.
Assignment operator(=) replaces the current value in the variable.
Always initialize variable because garbage value causes unconditional behaviour.
+ 2
It gives 20 output
+ 2
Jayakrishna🇮🇳 i also think that but my friend is arguing 1st print will give garbage value as it has not any value initilised before printing. So i am not sure about this
+ 1
`Int` is unknown type (first line).
Unless you specify an appropriate format specifier for printf() call, it triggers error.
Having all the issues been fixed, first printf() call outputs garbage value.
+ 1
Ipang in c language default value comes into picture or not? Like default value of int is 0.
+ 1
020
it first search and accept local variable. If there is no local variable search for global variable if no 'a' locally.....
Edit: it prints 0 but it gives warning also, you can see.. Uninitialized variable access..
+ 1
Jyoti Rani
Not always, local variables are not initialized. Global or static variables are initialized to default value.
+ 1
Ipang but you can see the code i edited question there it is printing 0😣
+ 1
If it giving you warning, then it means it is unpredictable.
That's a compiler optimisation but if you run in other compiler then you may not get same results. So it tells you to best initialize before using it..
If you run code with Arrays then you can see garbage values,..
When it comes to Arrays,..
Check this, :
int main() {
int a[20];
printf ("%d\n",a[0]);
printf ("%d\n",a[5]);
printf ("%d",a[8]);
printf ("\n");
printf ("%d",a[9]);
return 0;
}
You may not same result everytime, if you run this..
Thats only thing to remember.....@Ipang means
+ 1
No problem Jyoti Rani
Keep coding and stay curious 👍
+ 1
Jyoti Rani
That's a right way, you found it practically...
You're welcome..
+ 1
The scope of var a has been redefined to LOCAL..
Remove the reinitialization statement ...
+ 1
Navneet Kaur💫 this is just a question to check your knowledge not one do like this in their program and the fact is
Unlike some programming languages, C/C++ does not initialize most variables to a given value (such as zero) automatically. Thus when a variable is assigned a memory location by the compiler, the default value of that variable is whatever (garbage) value happens to already be in that memory location!
Different compiler will respond in different way some will give you zero some will give garbage. I saw the standard dennis ritchie book which also says access of non initialise variable gives garbage value. So i am going with that otherwise it depends on you.
+ 1
DEATH128 thanks
0
In first default value of int will be printed due to no initialization.
In the second case ,20 because you initialize variable a.you can also use scanf ("%d",&a) to take user input.
0
After declaration of int a in line 2 u declared it again in line 4 but without any value.
The default value is 0.
So it outputs 0 first then 20.