Thread: putchar vs puts

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    putchar vs puts

    Can someone explain the difference between putchar and puts functions (also getchar and gets)?

    I am thinking that putchar will only one character and
    puts writes a whole string

    putchar (p) is not legal and
    puts(*p) is not legal

    Is this correct??? If not please help me with this.
    Sue B.

    dazed and confused


  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    I am thinking that putchar will only one character and
    puts writes a whole string

    your right... the same goes for getchar and gets


    putchar (p) is not legal and
    puts(*p) is not legal

    that depends on what p is i assume a char poiner

    Example:

    Code:
    int main(void)
    {
        char* p = NULL;
        char s[81] = "WOO STRING";
        p = s;
        putchar(*p); // should output 'W'
        puts(p); // should output 'WOO STRING'
        *p = getchar(); // get a single char;
        putchar(*p); // print the char we just input
        gets(s); // geta line 
        puts(s); // print the line
        return 0;
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Here are the different permitations to printing to the display........

    Code:
    char str[]="HELLO PEOPLE";
    char *s_ptr; 
    
    for(s_ptr = str; *s_ptr; s_ptr++)
    putchar(*s_ptr); /*will print single character each loop*/ 
    printf("\n\n");
    /*reset pointer*/
    s_ptr = str;
    
    puts(s_ptr); /*will print HELLO PEOPLE*/
    
    puts(str); /*will print HELLO PEOPLE*/
    
    printf("\n%s", s_ptr); /*will print HELLO PEOPLE*/
    
    printf("\n%s", str); /*will print HELLO PEOPLE*/
    Last edited by bigtamscot; 09-20-2001 at 06:37 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Unregistered
    Guest
    putchar is used for a single character
    puts is used for a string.
    Remember many of the reserved words in C are short forms of words such as putchar(put character)
    puts(put string)
    Yilmaz
    (Just finished C course)

  5. #5
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    so I can safely say ...........

    in your example if putchar was used instead of puts:

    Code:
    char str[]="HELLO PEOPLE";
    char *s_ptr; 
    
    for(s_ptr = str; *s_ptr; s_ptr++)
    putchar(*s_ptr); /*will print single character each loop*/ 
    printf("\n\n");
    /*reset pointer*/
    s_ptr = str;
    
    puts(s_ptr); /*will print HELLO PEOPLE*/
    
    puts(str); /*will print HELLO PEOPLE*/
    putchar(s_ptr); /* will only print H */
    putchar(str); /* wouldn't be legal */
    Sue B.

    dazed and confused


  6. #6
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157


    lets say you put the following:

    Code:
    char *ptr = "WOO STRING";
    
    putchar (ptr);   // is not valid
    putchar (*ptr);   // output :  W
    puts(ptr);     // output :  WOOD STRING
    puts (*ptr);   //  is not valid
    ptr is the string RIGHT?
    *ptr is pointing to first character only of the string RIGHT??
    Sue B.

    dazed and confused


  7. #7
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    yes exactly.
    though doing this ptr++ or ptr + 1 would make *ptr the second character of the string, and cause puts(ptr) to skip the first 'W'

    im gonna edit in an Example to make this more clear

    Code:
    int main(void)
    {
        char* p = NULL;
        char s[81] = "WOO STRING";
        p = s;
        putchar(*p); // should output 'W'
        puts(p); // should output 'WOO STRING'
        p++; // advance the pointer 'p' to s[1] or 'O' the second char in the string 's'
        putchar(*p) // should output 'O'
        puts(p) // should output 'OO STRING'
        return 0;
    }
    Last edited by no-one; 09-20-2001 at 11:04 AM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  8. #8
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    Wink THANKS

    I ran your code 'no-one' and played with it a bit to see what output I get. I understand.

    puts also automatically adds a \n (new line) at the end.
    That is helpful.
    Last edited by sballew; 09-20-2001 at 08:13 PM.
    Sue B.

    dazed and confused


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. problem with struct
    By nevrax in forum C Programming
    Replies: 13
    Last Post: 05-02-2007, 11:38 AM
  3. crazy triangles
    By markg in forum C Programming
    Replies: 3
    Last Post: 10-24-2005, 12:50 PM
  4. Text based frame in win32 console
    By GaPe in forum C Programming
    Replies: 8
    Last Post: 04-16-2002, 07:01 AM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM