0
#include <stdio.h>  
int main()
{
    int i,j=3;
    i=4+2*j/i-1;
    printf("%d",i);
    return 0;
}

It will print 9 every time,though i is not initialized, So, it must print any garbage value. Please Explain...

banarun
  • 2,305
  • 2
  • 23
  • 40
user2536783
  • 95
  • 1
  • 2

4 Answers4

10

The value of an uninitialized local variable in C is indeterminate and reading it can invoke undefined behavior.

Now, repeatedly executing a particular program compiled with a particular compiler in a particular environment (as you are doing) is likely to yield the same (still undefined, of course) behavior. This could be because the OS will usually give your process the same range of logical memory every time you run it and thus the garbage that your program reads has a good chance of being the same every time (but it's still garbage, nonetheless). Or it could be because the compiler doesn't even bother to give you a binary representation of the garbage you would be reading and instead gives you something else (as long as it doesn't violate the standard).

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
2

Your code will result in Undefined Behavior. Undefined behavior refers to computer code whose behavior is unpredictable. The output of the code depends on the compiler, environment.

Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49
2

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf is the International Standard for C Programming languages

Page No : 126

Heading : Semantics

Item No : 10

Quoting from that

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

This must answer your question.

EDIT : Suggested by @Jens Gustedt in the comments

6.3.2.1, p2, says If the lvalue designates an object of automatic storage duration that could have been declared with the register storage class (never had its address taken), and that object is uninitialized (not declared with an initializer and no assignment to it has been performed prior to use), the behavior is undefined.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • 1
    The official standard is C11, document 1570. And this section that you cite isn't sufficient. 6.3.2.1, p2, says *If the lvalue designates an object of automatic storage duration that could have been declared with the register storage class (never had its address taken), and that object is uninitialized (not declared with an initializer and no assignment to it has been performed prior to use), the behavior is undefined.* – Jens Gustedt Jun 30 '13 at 16:38
  • Does that imply that if an automatic variable does have its address taken but is never written, it may safely be read multiple times and is guaranteed to contain the same value every time, without invoking Undefined Behavior? Does that apply to types larger than `char`? – supercat Jun 30 '13 at 17:08
  • @JensGustedt This is slightly off topic, but are you aware of anywhere I can find a list of differences between C99 and C11 and/or a list of differences between N1570 and the official final C11? – zwol Jul 01 '13 at 19:09
  • @Zack, on the site of the committee http://open-std.org/jtc1/sc22/wg14/www/docs/ you find pdfs that have the differences from previous drafts and so on. n1570 itself has such diff markers. Probably you could find which is diffed to which from the mailings that should also be published, there. The difference of n1570 to the final version are basically just this diff markers that were taken out. There is already a TC to the standard that just specifies the value of `__STDC_VERSION__` to `201112L`, that had been forgotten previously. – Jens Gustedt Jul 01 '13 at 19:42
  • @JensGustedt I was hoping for something more like a detailed list of changes since C99. (N1570 does have an overview, but not nearly enough detail for me: I effectively memorized C99 back when I was Mr. Preprocessor for GCC, so if I just sit down and try to read C2011 I will fail to notice changes, changebars or no.) – zwol Jul 02 '13 at 00:41
1

When a variable is used before its initialization, it would take a garbage value from memory.
A Garbage value is a value last stored in a memory location reserved for that variable (in this case i).

When you compile your program, every time it will fetch that previous stored value from that memory location and will result in an undefined behavior.
It is not necessary it will give the output 9 every time. The program may behave differently when compiled with different compilers.

Ry-
  • 218,210
  • 55
  • 464
  • 476
haccks
  • 104,019
  • 25
  • 176
  • 264
  • Well, not necessarily, but in the case of the GNU C compiler, I believe this is true. The variable is given an address, and its value will simply be whatever was already in that memory location. – Noldorin Jun 30 '13 at 16:25
  • I didn't down-vote you mate. I'll up-vote you now in fact, because a down-vote is too harsh. – Noldorin Jun 30 '13 at 16:47
  • @Noldorin;I have edited my post regarding your first comment. And reason for down vote.......? is for down voter – haccks Jun 30 '13 at 17:00