0

In the below code output is "garbage 300" but I am expecting this as "0 300"
why this is happening?

    main()
    {
    int a = 300, b, c ;
 if ( a >= 400 )
    b = 300 ;
    c = 200 ;
    printf ( "\n%d %d", b, c ) ;
    }

while in below code it is giving expected output as- 0 200

main( )
{
int a = 300, b, c ;
   
c = 200 ;
printf ( "\n%d %d", b, c ) ;
} 
  • 3
    Because `b` is assigned only in case `a>=400`. And it is not. Proper indentation would help here. – Eugene Sh. Nov 09 '20 at 21:02
  • 1
    @Divyank Lalwani Uninitialized variables with automatic storage duration have indeterminate values. – Vlad from Moscow Nov 09 '20 at 21:03
  • 2
    Why did you change your code in the question? In this one it is clear that you are printing uninitialized variable (`b`). There is no "second program" as the title is mentioning. – Eugene Sh. Nov 09 '20 at 21:04
  • @EugeneSh I apologize for that,I am new on stack overflow. now I edited that please see it. – Divyank Lalwani Nov 09 '20 at 21:08
  • `main()` must return `int` and take arguments `int argc, char** argv`. – underscore_d Nov 09 '20 at 21:08
  • Anyway, your question was addressed in first 3 comments – Eugene Sh. Nov 09 '20 at 21:09
  • 1
    Undefined behaviour is undefined. Dupe of [Undefined, unspecified and implementation-defined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) – underscore_d Nov 09 '20 at 21:09
  • @VladfromMoscow I did not understand what you want to say. – Divyank Lalwani Nov 09 '20 at 21:11
  • 1
    `0` is a perfectly valid garbage value ... as valid as `-8002413217` – pmg Nov 09 '20 at 21:15
  • @DivyankLalwani Your programs have undefined behaviors because you are outputting an uninitialized variable with an indeterminate value. – Vlad from Moscow Nov 09 '20 at 21:15
  • @DivyankLalwani Uninitialised variables cannot be read, only assigned. Objects in C do not have default values if no initial value is provided. So, `b` and `c` have indeterminate values and cannot be printed until they have been assigned-to. They do _not_ default to `0`. – underscore_d Nov 09 '20 at 21:22

1 Answers1

1

Automatic variables are not automatically initialized. Reading them before writing is undefined behavior. For integers, you usually get some garbage value (whatever happens to be on stack).

Your first program is

main()
{
    int a = 300, b, c;
    if (a >= 400)
        b = 300;
    c = 200;
    printf("\n%d %d", b, c) ;
}

The condition is wrong, so b does not get assigned a value (it stays uninitialized). That's why you get the garbage.

The second program is essentially the same. b happens to have the garbage value 0 in that case.

Automatic variables are all variables defined in a function without storage class specifier static or extern.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
  • 1
    "_Automatic variables are not automatically initialized._" That's a deliciously ironic way of putting it. I'll have to remember that. – underscore_d Nov 09 '20 at 21:23