Use of puts(str) vs printf("%s", str);

This is a discussion on Use of puts(str) vs printf("%s", str); within the C Programming forums, part of the General Programming Boards category; Both puts(str) and printf serves the same purpose. Why puts(str) is usually preferred in string than printf? Thanks!...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    18

    Use of puts(str) vs printf("%s", str);

    Both puts(str) and printf serves the same purpose. Why puts(str) is usually preferred in string than printf?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    Quote Originally Posted by wonderwall
    Both puts(str) and printf serves the same purpose. Why puts(str) is usually preferred in string than printf?
    Preferred? If all you want to do is print a string with a newline, then puts does that nicely. When other formatting comes into play, puts won't cut it without more work on your part.

    Note that printf("%s\n", str) would be the equivalent of puts(str).
    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

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    18
    Ok thanks! If i dont want to print with a newline, i can just use printf("%s", str); right?
    And it is necessary to have this step after fgets
    Code:
    fgets(str, LENGTH, stdin);
    str[strlen(str) -1] = '\0';
    what is the purpose? Sorry for having so many questions! I hope to get the concepts right!
    Last edited by wonderwall; 11-06-2011 at 08:38 AM. Reason: typo

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    Quote Originally Posted by wonderwall
    If i dont want to print with a newline, i can just use printf("%s", str); right?
    Yes.

    Quote Originally Posted by wonderwall
    And it is necessary to have this step after fgets (...) what is the purpose?
    I answered this in your fgets vs gets thread. Note that strictly speaking, just setting the character at the strlen(str) - 1 index to be a null character is wrong because if the line read is long enough, the newline will not be written to the buffer because it would not even be read.
    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. Printf error "address pointing at code space is taken"
    By KMAN999 in forum C Programming
    Replies: 2
    Last Post: 07-04-2011, 03:27 PM
  2. Replies: 8
    Last Post: 08-23-2009, 01:33 PM
  3. printf("04x",short) - negitive hex giving me problems
    By KairosDrasis in forum C Programming
    Replies: 3
    Last Post: 12-09-2008, 10:51 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21