Home > Net >  Is this undefined behavior? why does this code return a seemingly random integer?
Is this undefined behavior? why does this code return a seemingly random integer?

Time:12-28

I'm looking at the following C code:

#include <stdio.h>
#include <stdlib.h>

void main(){
    int a;
    printf("%d", a);

}

I'm wondering why this code prints a seemingly random number. My first guess is that this is undefined behavior but I could be wrong.

CodePudding user response:

because you didn't assigned any value to a, so this is giving garbage value every time you run code.

CodePudding user response:

Yes, it is undefined behavior.

This question seems to be a duplicate of Reading uninitialized variable

  • Related