char a[3]={'U','S','A'};
cout<<a;
How can it Print USA when a is character array of length 3 and there is no for memory '\0' ?
Where will be '\0' stored ?
cout will print until it encounters a \0.
Now if it happens to be that in the physical memory, the byte next to your array has the value 0, cout will take it as the terminator. It can happen as the next byte may have any garbage value, including 0.
However, there is no guarantee what will be next to your array's boundary. In one case you have found 0 does not mean it will continue for other cases too. The actual outcome is undefined behaviour, which, in this particular case, matched with the expected behaviour of cout in case the array was null-terminated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With