Thread: printf() vs puts() behavior

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    printf() vs puts() behavior

    When I do the following

    m-net% more mod.c
    Code:
    #include <stdio.h>
    
    int main (void)
    {
       char str[] = "Aamit";
       *str='R';
       puts(str);
       return 0;
    }
    The output is fine.
    Code:
    m-net% gcc -g -Wall mod.c -o mod
    m-net% ./mod
    Ramit
    However, when I replace puts() with printf()

    m-net%m-net% more mod.c
    Code:
    #include <stdio.h>
    
    int main (void)
    {
       char str[] = "Aamit";
       *str='R';
       printf("%s \n " ,str);
       return 0;
    }
    I get some junk afterwards.

    Code:
    m-net% gcc -g -Wall mod.c -o mod
    m-net% ./mod
    Ramit
     %                                                                              
    m-net%
    Whis is this?
    Last edited by Dave_Sinkula; 05-29-2008 at 04:25 PM. Reason: Added code tags to preserve the leading space in the output.

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    22
    *str == str[0]
    Pointers point to the first element of an array. So when you dereferenced str you were assigning 'R' as the first element of the array.

    printf prints all the characters in an array until it reaches the \0 character. You need to null out the rest of the characters in the array if you do not want them to print.

  3. #3
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Then how comes this doesn't happen with puts()?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    So the real problem is the percent prompt at the end yes?

    Well I'm not sure it's entirely printf's fault, but it does have a different job then puts. puts doesn't flush stdout after printing, so it could be that you are only seeing this after the program has closed and the buffers have been flushed in the first example.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >   printf("%s \n " ,str);
    Try removing the space after the newline in the above printf(), and see if you still have the problem.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    What is your prompt set to?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by gwarf420 View Post
    printf prints all the characters in an array until it reaches the \0 character. You need to null out the rest of the characters in the array if you do not want them to print.
    The string is already null terminated... The *str line doesn't change that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > Then how comes this doesn't happen with puts()?
    Because puts() puts a new line on the end for you...

    Swoopy has already answered your question IMO.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM