Could anyone please tell me how “hello” is being printed here? As far as my knowledge goes, local variables are uninitialized. Therefore, I thought the else part should be printed but it’s happening otherwise. Please help.
struct buss{
int a;
char b;
};
void main(){
struct buss *p;
if(p==NULL) printf("hello");
else printf("hi");
}
You didn’t initialize
pobject. Its value is indeterminate.Reading an uninitialized object is undefined behavior. Undefined behavior means anything can happen. Anything means your program can also crash or print
"nose demons".EDIT: as requested in the comments, to the question “is reading an uninitialized object always undefined behavior?”: for C90, see 3.16 in the definition of undefined behavior, for C11, see 6.3.2.1p2 and for C99, see the C Committee answer in DR#338.