Thread: printing strings

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    24

    printing strings

    If you make a null-terminated string, then call printf() on it, will the null character cause a gap at the end of it?

    What happens if you call printf() and the string isn't null-terminated?

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    7
    Quote Originally Posted by Tom_Arch View Post
    If you make a null-terminated string, then call printf() on it, will the null character cause a gap at the end of it?
    Not sure what you mean by causing gap at end, but the printf() will not print the NULL explicity, it just prints upto NULL.

    Quote Originally Posted by Tom_Arch View Post
    What happens if you call printf() and the string isn't null-terminated?
    Depending on what's there next to your string in memory, it might print junk or bail out with overflow.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Tom_Arch
    If you make a null-terminated string, then call printf() on it, will the null character cause a gap at the end of it?
    No, it will not since the null terminator is not printed.

    Quote Originally Posted by Tom_Arch
    What happens if you call printf() and the string isn't null-terminated?
    Undefined behaviour unless you also specify a precision that is less than the number of characters in the array. Note that without a null terminator, it is not a string, by standard definition.

    EDIT:
    Quote Originally Posted by sbattu
    Not sure what you mean by causing gap at end, but the printf() will not print the NULL explicity, it just prints upto NULL.
    From context and in text, Tom_Arch is talking about a null character, not NULL.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    Quote Originally Posted by laserlight View Post
    From context and in text, Tom_Arch is talking about a null character, not NULL.
    What's the difference?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Tom_Arch
    What's the difference?
    NULL is a macro for a null pointer constant. Although in terms of integer value they are equal and may even have the same type, the null character clearly is not (at least semantically) a null pointer constant.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing wide characters to strings...
    By davo666 in forum C Programming
    Replies: 1
    Last Post: 02-21-2009, 12:56 AM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. printing char* strings
    By difficult.name in forum C Programming
    Replies: 4
    Last Post: 12-10-2004, 07:06 PM
  4. printing strings (was similar problem)
    By weirdbeardmt in forum C Programming
    Replies: 5
    Last Post: 06-01-2004, 01:12 PM
  5. printing an array of strings
    By linucksrox in forum C Programming
    Replies: 3
    Last Post: 05-11-2004, 03:31 PM