Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
5 replies to this topic

#1   Members   -  Reputation: 303

Like
0Likes
Like

Posted 02 March 2014 - 10:23 PM

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.


Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.

#2   Members   -  Reputation: 2389

Like
7Likes
Like

Posted 02 March 2014 - 10:29 PM

*
POPULAR

Uninitialized data is sometimes 0. It's chance. Don't rely on it.



#3   Moderators   -  Reputation: 9433

Like
0Likes
Like

Posted 02 March 2014 - 10:29 PM

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.
gzip: H4sIAAAAAAAEAG1QTUvEMBC991e8nvaiFfYoS7yo sLCo6MnjtJ1ugmkiyWRL/72z3T1YEQIJ8z4zA2Xp yPvt1qBpGrRFIJZkk9FyRyUzHCbKIHgn4hnZOrm1 TD0mG0HCCs+QGDGWziKXI6Wm2n++GYwUVH2mrGEE PnGCVQ8K8+JYfXA6URDEQfMZh5h6g5eoAlWJdeEI bbH2qYZf7XMUfw8f/Q0oMeZYNL9/WHF0uFEshvMr XYujd9SycFb+F18QcSOvlJauZ8ejqevdnV7/d550 e0t6prmunh73Bu+vz4c/XUeOQXfJgvKNkhf95U8/ Dtgmy5IBAAA=

#4 slalrbalr   Banned   -  Reputation: 2005

Like
0Likes
Like

Posted 03 March 2014 - 01:55 AM

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).



#5   Members   -  Reputation: 303

Like
0Likes
Like

Posted 03 March 2014 - 06:57 PM

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.


Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.

#6   Crossbones+   -  Reputation: 11717

Like
5Likes
Like

Posted 04 March 2014 - 02:21 AM

*
POPULAR

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.


Edited by Aardvajk, 04 March 2014 - 02:23 AM.





Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.




PARTNERS