0

if i run this program:

#include <stdio.h>

int main()
{
    int a=32,b=2,c,i;
    for(i=0;i<3;i++){
        printf("%d\n",c);
        c=a/b;
        a=c;
    }

    return 0;
}

the output is:

32765
16
8

In there, I dont define the value of C, Where from came this output 32765.? even again i run this code more time,it show different values like 32764,32767. Why this different output showing on?

Rakibul Islam
  • 167
  • 1
  • 2
  • 14
  • 1
    An automatic variable is generally created on the stack and is not initialized. So it carries the previous value of that place on the stack. This is undefined behavior. – Paul Ogilvie Apr 12 '19 at 13:22
  • The value of uninitialized local non-static variables (like `c`) is *indeterminate*. It can be seen as random or garbage. And depending on the type, using the variable except for initialization (i.e. as target of an assignment) could lead to *undefined behavior* (if the "random" value happened to be a *trap representation* of the type). – Some programmer dude Apr 12 '19 at 13:23
  • @PaulOgilvie: (a) “Undefined behavior” has a specific meaning in the C standard. Merely using an uninitialized object does not *per se* constitute undefined behavior. (b) Even if an object is allocated on the and is not initialized, it is not correct to say “it carries the previous value of that place on the stack.” Its value is indeterminate, and it may behave as if it has a value different from the contents of the memory allocated for it. – Eric Postpischil Apr 12 '19 at 13:57
  • The more important question would be: "If you do not initialize the variable (but you define it) why would you expect not to get a certain value printed?" – Gerhardh Apr 12 '19 at 14:15
  • @EricPostpischil, I know, I know...sometimes I am just a bit simplistic, also to make OP understand... – Paul Ogilvie Apr 12 '19 at 14:17

2 Answers2

1

Because c has automatic storage duration (i.e. is a non-static local variable) and is uninitialized, its value is indeterminate. Attempting to print an uninitialized variable that never had its address taken (i.e. was not the subject of the address-of operator &) invokes undefined behavior.

Even if you did take the address of c you could still have undefined behavior if it contains a trap representation. If it does not contain a trap representation (and most implementations don't have them), the the value is unspecified which simply means the printed value can't be predicted.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

Automatic variables (a local variables which are allocated and deallocated automatically when program flow enters and leaves the variables's scope) for which there is no explicit initializer have undefined (i.e. garbage) values.

Kobip
  • 114
  • 1
  • 8
  • “Undefined” has a specific meaning in the C standard. Uninitialized objects have *indeterminate* values. Also, although you are on the right track with scope, the lifetime of an object with automatic storage duration ends when execution of its associated block ends, not merely when execution leaves the block. For example, execution temporarily leaves the block when it calls a subroutine, but the automatic objects continues to exist. Also, for variable length arrays with automatic storage duration, their lifetime begins when execution reaches their declaration, not when it enters their block. – Eric Postpischil Apr 12 '19 at 13:53