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.
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.
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).
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.
Someone better ask...
Any particular reason for using printf in a C++ program? I see C++ in the tags. One of the advantages of using std::string and std::cout is that you don't have to worry about this sort of thing, along with the type-safety the iostreams give you over printf's formatting strings.
15 years ago, I felt that null terminators were a big part of my life. I haven't had to think about them for many years.