If a variable is not initialized, when does it throw as an error and when does it give a garbage value?

I used the following codes using ideone.com :

#include <stdio.h>

int main(void)
{
    int i,j=0;
    if(j)
    {
        j=0; //To suppress the warning that j is not used
        i=1;
    }
    printf("%d\n",i);
}

Output: Garbage Value:

 #include <stdio.h>

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

Conclusion:

Error: using a unified local variable I.

Is this because the compiler completely removes the if (0) block during optimization? And such an optimization cannot be performed in the case of if (j), since this is a variable? Will the j value be present at compile time and should the same optimization be done? Or is it that memory is allocated only at runtime?

+4
source share
2 answers

, Ideone gcc, -Werror, - Wuninitialized , -Werror, . , gcc, :

, , , GCC.

, , , , .

- Coliru, , .

undefined , , undefined quesiton 4 ?.

+3

ISO/IEC: 9899 TC3

5.1.1.3 1 ( ), , undefined . .8)

, , , undefined, .

, , - . , , , , .

+2

Source: https://habr.com/ru/post/1545692/


All Articles