-3

Why do I get the output for i2 as 1 in DevC++ while in other places i get 0?

#include <stdio.h> 
int main(void) {
    int i1,i2;
    char c1,c2;
    float f1,f2;
    long l1,l2;
    double d1,d2; 
    printf("\n%d %d",i1,i2);
    printf("\n%c %c",c1,c2);
    printf("\n%d %d",f1,f2);
    printf("\n%l %l",l1,l2);
    printf("\n%lf %lf",d1,d2);
    return 0;
}
  • *Yet another* question on the same matter: https://stackoverflow.com/questions/32140438/what-is-the-default-value-of-a-local-variable-in-c – Arkadiusz Drabczyk Aug 21 '15 at 13:01
  • possible duplicate of [What happens to a declared, uninitialized variable in C? Does it have a value?](http://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value) – halfzebra Aug 21 '15 at 13:10
  • More garbage. As well as the uninitialized automatic UB, there is the printf("%d....' on a float UB. – Martin James Aug 21 '15 at 13:15
  • I'm voting to close this question as off-topic because OP wants an explanation for two different UB's in a 14-line program. This is multi-duped homework garbage and of zero, or negative, use for future SO visitors. – Martin James Aug 21 '15 at 13:19

3 Answers3

2

You did not initialize any of the variables which leads to Undefined behavior when you are printing them. Note that, when you declare

int i1,i2;
....

and leave them uninitialized, they contains garbage values.

mazhar islam
  • 5,561
  • 3
  • 20
  • 41
2

Automatic objects that have not been initialized or assigned a value have an indeterminate value, that is either an unspecified value or a trap representation.

ouah
  • 142,963
  • 15
  • 272
  • 331
0

Yes they contain garbage values. But why all other are printing 0 but only i2 is printing 1. If it would have been a garbage value then for every execution the values would vary.