puts( );

This is a discussion on puts( ); within the C Programming forums, part of the General Programming Boards category; please forgive the simplicity of the question, but I haven't seen this one line of code yet. I found this ...

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    puts( );

    please forgive the simplicity of the question, but I haven't seen this one line of code yet.
    I found this line of code at
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    searching for in on google and this forum brings up too many literal uses of the word puts, but not the use of it as code.
    from what i read, it looks like it just outputs (prints) to the default device (monitor)
    is this correct? does it do anything else? does anyone know what the code of the function looks like?
    is it overloaded with other capabilities?
    Code:
      for (i = 0; i < num; i++)
    	puts ("Looping");
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You're correct, puts outputs a string to stdout which is usually the monitor. It will only except a pointer to a string. The code of the function depends on the implamentation of the c language, I would imagine it is very simple, the function is probably just a wrapper around some OS function to output text.

  3. #3
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    i'm assuming from the simplicity of the function, that it is a portable function.
    Is this correct?
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    does anyone know what the code of the function looks like?
    I'm guessing something like:
    Code:
    int puts(const char *str)
    {
      char *s;
    
      for(s = str;*s;s++)
        if(fputc(*s, stdout) == EOF)
          return EOF;
    
      return 1;
    }
    If you understand what you're doing, you're not learning anything.

  5. #5
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    puts() also prints the new line character at the end for you. So you could type:
    Code:
    puts("hello world!");
    instead of
    Code:
    printf("hello world!\n");

  6. #6
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    is it standard (portable)?
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Yes, puts is part of the c standard.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    If you have seen fputs() before, I tend to just think of puts() as fputs(const char *s, stdout). So you just don't need to specify where the stream goes to since it always defaults to stdout. Much like printf() and fprintf() etc.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    Except, puts() appends a newline and fputs() doesn't.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    473
    Searching google for something like "man puts()" usually helps for function definitions/syntax declaration.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with struct
    By nevrax in forum C Programming
    Replies: 13
    Last Post: 05-02-2007, 11:38 AM
  2. Puts() or Printf() ?
    By hern in forum C Programming
    Replies: 5
    Last Post: 06-25-2003, 06:09 AM
  3. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 06:53 AM
  4. Custom gets and puts functions
    By Silverdream in forum C Programming
    Replies: 2
    Last Post: 02-18-2002, 02:12 PM
  5. putchar vs puts
    By sballew in forum C Programming
    Replies: 7
    Last Post: 09-20-2001, 02:02 PM

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