[Solved] Why uninitialized char array is filled with random symbols?


what is the reason behind this output?

The values in an automatic variable are indeterminate. The standard doesn’t specify it, so it might be spaces as you said, it might be random content.

[…] sometimes not fully filled and I left with junk […]

Strings in C are null-terminated, so any routine dedicated to printing a string will loop as long as no null byte is encountered. In uninitialized memory, this null byte occurs randomly (or not at all). These weird, trailing characters are a result of that.

is there any way to avoid this?

Yes. Initialize it.

1

solved Why uninitialized char array is filled with random symbols?