0
void main()
{
    int i, n, count = 0;
    printf("%d\n", i);
    printf("%d\n", n);

    while (count != 10)
    {
        printf("%d", i);
        i++;
        count++;
    }
}

enter image description here

IF it is giving me some garbage value then how can be the same value be assigned to 'n' variable?

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 2
    What value do you expect instead of 0? – klutt Aug 16 '20 at 20:31
  • 2
    Unrelated - The return type of `main` must be `int`. – anastaciu Aug 16 '20 at 20:41
  • @klutt Possibly garbage one, as anyone should. Anyway, memory you get from modern OS is always zeroed out, and that's why everything in trivial examples like these will pretty much always have initial values zeroed out the very first time. – Yamirui Aug 16 '20 at 20:44
  • @Yamirui Is not 0 a member of the set of garbage values? ;) – klutt Aug 17 '20 at 07:33
  • @klutt I don't understand the point of your question, I just pointed out why it is seemingly consistently "always" 0 instead of assuming that they know where stack appears from. – Yamirui Aug 17 '20 at 07:43
  • @Yamirui I think we are meaning the same thing. My point is that you cannot really expect anything from garbage values. They are even more unpredictable than random numbers, since random numbers at least are expected to have some kind of defined distribution. – klutt Aug 17 '20 at 07:48
  • "Garbage" is kind of the wrong term to use because it gives the impression that the value is always going to unusable, which isn't the case. "Indeterminate" is a better term. In short, you should not be *surprised* to see an "expected" value in an uninitialized `auto` variable, just don't assume it's set that way for any particular reason. – John Bode Aug 17 '20 at 15:42
  • For future questions please do not add pictures of text. Text output is no artwork that requires a screenshot. Simply do copy&paste to add the text directly into the question is prefered. – Gerhardh Aug 18 '20 at 08:40
  • @Gerhardh Okay. Noted Sir! – Mohsin Khan Aug 19 '20 at 09:10

1 Answers1

1

As you already observed, the value of i is indeterminate. It happens to be zero in your environment. With a different compiler, it might happen to be something else. There's no mystery there, just happenstance.

If it is giving me some garbage value then how can be the same value be assigned to 'n' variable?

It can't, reliably. (It will work on some compilers, and on most with optimization disabled, but in general, you can't reliably duplicate an uninitialized variable's indeterminate value.)

Howlium
  • 1,218
  • 1
  • 8
  • 19
  • 2
    The value is *indeterminate*. Reading it is *undefined behavior*. So no, your line `n = i` does not work. – klutt Aug 17 '20 at 07:35
  • The question was, “IF it is giving me some garbage value then how can be the same value be assigned to 'n' variable?” You seem to be saying that assigning the value to n won't give n the same "garbage value." Can you provide more information about how, after that assignment, the values could be different? – Howlium Aug 17 '20 at 08:06
  • 1
    What I'm saying is that, since `i` has an indeterminate value, `n=i` will invoke UB. – klutt Aug 17 '20 at 08:25
  • 1
    Which means that this: `int main() { int i; int n=i; printf("%d %d", i, n); }` may theoretically print two different numbers. Or print nothing. Or may do something completely different. – klutt Aug 17 '20 at 08:41
  • @Howlium My question is that.. The '0' assigned to 'i' is a garbage value or not... and if it is.. is it possible to assign the same garbage value to other cuz earlier when I used to code in C.. the garbage values assigned to every variable was different.. I hope you guys get my question – Mohsin Khan Aug 17 '20 at 13:19
  • @klutt what's UB? – Mohsin Khan Aug 17 '20 at 13:20
  • @MohsinKhan Undefined behavior – klutt Aug 17 '20 at 13:23
  • @klutt I'm not asking how yo assign the values of 'i' to 'n'.. I'm asking how two different variables can have the same garbage values.. as you can see in the program '''printf("%d\n", i); printf("%d\n", n);''' and the output coming for 'i' as well as 'n' is coming '0'? – Mohsin Khan Aug 17 '20 at 13:26
  • @MohsinKhan I assumed that much, so this answer does not answer your question. Please note that it's Howlium that has answered and not me. But the short answer to why both variables are 0 is that there are no requirements on their values and using their values is undefined behavior. – klutt Aug 17 '20 at 13:30
  • @MohsinKhan Basically, your question is equivalent to "I tossed two dice. Both showed 4. Why?" – klutt Aug 17 '20 at 14:06
  • @klutt, not to be pedantic, but that's incorrect. Yes, reading an uninitialized variable is undefined behavior because you cannot predict the result of that behavior. But you DO know that when you read an uninitialized variable, the result WILL be the same value as that of the variable that was read. And the question asked was how to get those two values to hold the same unpredictable value. – Howlium Aug 18 '20 at 02:57
  • @Howlium Sorry, but you're showing that you simply don't know what undefined behavior is. When you invoke undefined behavior, the standard puts NO constraints on the compiler. Furthermore, the optimizer is free to assume that undefined behavior will never occur. – klutt Aug 18 '20 at 06:03
  • 1
    @Howlium Explain the output from this program https://godbolt.org/z/oojGh7 – klutt Aug 18 '20 at 07:13
  • Okay I have learned something today. Thanks. I was pretty certain you were wrong, too! – Howlium Aug 18 '20 at 07:18
  • @Howlium That's good. UB is weird and makes programs hard to debug. – klutt Aug 18 '20 at 09:04
  • @Howlium Downvote removed – klutt Aug 18 '20 at 11:06