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...

6
  • 16
    "It will print 9 everytime" Maybe in one particular environment with one particular compiler, but not in general. Commented Jun 30, 2013 at 16:19
  • 4
    For me it prints 3 every time. It is toolchain specific... BTW I've seen implementations of malloc that default memory to 0 even though the standard does not require it. Commented Jun 30, 2013 at 16:22
  • It will --- no it won't. Commented Jun 30, 2013 at 16:23
  • 2
    Floating point exception for me :) Commented Jun 30, 2013 at 16:23
  • Explain the down votes please Commented Jun 30, 2013 at 16:32

4 Answers 4

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Sssssssssshhhh. Be polite, guys!
Deleting everything doesn't seem to be the most adequate resolution.
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.

Comments

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.

5 Comments

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.
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?
@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?
@Zack, on the site of the committee 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.
@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.)
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.

3 Comments

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.
I didn't down-vote you mate. I'll up-vote you now in fact, because a down-vote is too harsh.
@Noldorin;I have edited my post regarding your first comment. And reason for down vote.......? is for down voter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.