3

If we declare a variable in the 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?

mielacademy
  • 109
  • 4
  • 7

5 Answers5

12

Yes. Initialise the variable.

pauljwilliams
  • 19,079
  • 3
  • 51
  • 79
  • 1
    I would only use intilization be compiler option (to different values, e.g. 0, 1, 0xFF) this for testing purposes. – Peter G. Aug 11 '10 at 09:33
  • 4
    @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 'SO- stop being evil' Aug 11 '10 at 09:35
  • 2
    You shouldnt use compiler options to work around a bug in your coed. And make no mistake, using an uninitialised variable *is* a bug. – pauljwilliams Aug 11 '10 at 09:42
  • 1
    Damn typo. Incidentally, I once had a bug in my coed, but some penicillin sorted it out. – pauljwilliams Aug 11 '10 at 09:52
1

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.

Andrey
  • 59,039
  • 12
  • 119
  • 163
Praveen S
  • 10,355
  • 2
  • 43
  • 69
1

At compile times, most compilers such as the GNU compiler can identify where an uninitialized variables is used. However, you may need to set flags, such as -Wall for the GNU compiler.

The value of the variable is already here though it could be any value. Namely it is the "initial" state of the variable.

Therefore, you must initialize the variable to avoid garbage.

When a variable x is declared, it already allocates a section to the memory &x which is referred by this variable name. Uninitialized values and variables are already placed in a memory address. Suppose you initialize a variable v of type int. It is allocated to a memory address, which is &v of type int *. Address &v therefore would be placed to an open unused place in the memory.

Consider this code inside a main function:

int x; 
// A number of bytes (in this case, sizeof(int), usually 4 B) already allocated
// starting at a memory location &x.

printf("Value at address %p: %d", &x, x); 
// Value at &x may be any int, which is unpredictable

When compiling this code, this warning message occurs, where SOME_DIRECTORY is an arbitrary directory:

SOME_DIRECTORY>gcc -Wall -g sampleprogram.c -o sampleprogram
sampleprogram.c: In function 'main':
sampleprogram.c:8:5: warning: 'x' is used uninitialized in this function [-Wuninitialized]
     printf("Value at address %p: %d", &x, x);

The starting value of a memory, just like circuits, is unpredictable. No matter why your value is random garbage. This is also a form of undefined behavior, which means that the International Standard of C compilers do not set any requirements so anything may happen. This is a very bad bug, can cause multiple hard-to-trace bugs and glitches.

1

If you are defining local variables then no. The compiler doesn't initialize them for you. The stack grows and shrinks depending on functions being called and their local variables. It doesn't make sense for a compiler to clear all the memory at every function call.

However, global variables can have an initial value of 0 if they are placed in the .bss this is an optimization to shrink the size of a program.

GH0S1
  • 56
  • 2
0

You certainly want to compile with all warnings and debug info: gcc -Wall -Wextra -g with GCC. Then it is likely that you'll get warnings, and you should improve your code to get none.

Of course, you should initialize your variables. BTW, such an initialization is short to code, and runs very quickly. And in several occasions (with e.g. -O1) gcc is able to optimize (using the as-if rule) and remove useless initializations. So as a rule of thumb don't be scared of "useless" initializations.

You should get the habit to initialize most variables (there are very few exceptions, e.g. the seed of some PRNG), actually all of them. In the few cases where you don't initialize a variable on purpose and prefer it to retain some garbage value, document that in a comment (but don't expect that garbage to be really random; it still could in practice be always the same).

Read more about undefined behavior. Be scared of UB.

Remember that variables are names in the source code. Variables don't exist at runtime (only locations exist then). They could be removed by the compiler, they might sometimes sit in some slot of the call frame of the call stack, they could go in some register, etc...

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547