Int a=10; void main(){int a; printf(a);a=20; printf(a);} | SoloLearn: Learn to code for FREE!

+2

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

c

7/22/2020 4:35:41 PM

Jyoti Rani

25 Answers

New Answer

+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

"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

Såñtösh Màràvi[Inactive] print is two times na one before initialisation and one after initialisation

+2

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 🤗

+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

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

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

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 [email protected] 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 ...

0

Ipang in c language default value comes into picture or not? Like default value of int is 0.

0

Ipang but you can see the code i edited question there it is printing 0😣

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.

0

Jyoti Rani hey! I should ask you why you take a twice ....as you've use int main and not defines the value of a and int returns a value 0 so it gives 0 value not garbage. And after that you defines a as 20 so it returns 20 value

0

You are initilazing int a again like new. Then it is being null i mean 0. If you dont write int a; secondly output will be 10 20