• Advertisement
Sign in to follow this  

Wasn't printf supposed to stop at '\0' ?

This topic is 1421 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Wasn't printf supposed to output to the console until encountering the null character?

 

I forgot to assign the null character to the end of the string yet it printed out the C array nicely.

Share this post


Link to post
Share on other sites
Advertisement
Yes, because it is the nature of C-strings. It just so happens that at the very specific time you ran your program, there was a zero byte after your string. Run your program in a debugger and examine the memory around your string. Run it again, then manipulate the memory around the string such that it doesn't end in a zero byte.

Share this post


Link to post
Share on other sites

Also, string constants already have a null-terminator, if that's what you assigned to the array you tested. You only have to add the null-terminator when you get the string from a source which provides non-null terminated strings (like functions that return a PASCAL-type strings, or an array and character count).

Share this post


Link to post
Share on other sites

Also, string constants already have a null-terminator, if that's what you assigned to the array you tested. You only have to add the null-terminator when you get the string from a source which provides non-null terminated strings (like functions that return a PASCAL-type strings, or an array and character count).

Or I wrote the function myself assigning each character individually to each array index.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement