views:

81

answers:

2

If we declare a variable in c programming as integer without defining value then printf prints some garbage value from buffer. Is there any method to prevent printing the garbage value?

I want to check if something can be done at compile time? if possible?

+10  A: 

Yes. Initialise the variable.

Visage
can something be done with complier options? i m using gcc
mielacademy
I would only use intilization be compiler option (to different values, e.g. 0, 1, 0xFF) this for testing purposes.
Peter G.
@mielacademy: always pass at least `-O -Wall` to gcc, then it will warn about common mistakes such as uninitialized variables. See also http://stackoverflow.com/questions/3375697/useful-gcc-flags-for-c
Gilles
You shouldnt use compiler options to work around a bug in your coed. And make no mistake, using an uninitialised variable *is* a bug.
Visage
Damn typo. Incidentally, I once had a bug in my coed, but some penicillin sorted it out.
Visage
A: 

Wikipedia says the following:

In computing, an uninitialized variable is a variable that is declared but is not set to a definite known value before it is used. It will have some value, but not a predictable one. As such it is a programming error and a common source of bugs in software

So initialize it to a default value.

Praveen S