puts/fputs

This is a discussion on puts/fputs within the C Programming forums, part of the General Programming Boards category; we know that puts automatically provides a newline character after the string and fputs doesn't do so. but i was ...

  1. #1
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    107

    puts/fputs

    we know that puts automatically provides a newline character after the string and fputs doesn't do so. but i was thinking that is it possible to have a newline character before the string while using puts or fputs, like we use in printf.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    You mean like: puts("\nThis is on a line by itself."); ?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    107
    yes. does this work? and let's say that i declare a string variable name then does puts("\n"name) works?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,308
    Quote Originally Posted by joybanerjee39 View Post
    yes. does this work?
    Try it and see if it does what you want.

    and let's say that i declare a string variable name then does puts("\n"name) works?
    Nope, the language doesn't allow concatenating strings together like that, no matter whether you're using puts, some other function or no function whatsoever. That sort of concatenation only works with literals, which are the things you put in double quotes " ". You need printf if you're mixing literals and variables.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by anduril462 View Post
    Nope, the language doesn't allow concatenating strings together like that,
    Right, but concatenating string-literals is allowed like
    Code:
    puts( "\n" "foo" " " "bar" );
    or
    #define TOSTR(s) #s
    #define PRENL(s) "\n" TOSTR(s)
    puts( PRENL( name ) ); /* where name is not a variable, it will be quoted to a literal */

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,308
    Quote Originally Posted by BillyTKid View Post
    Right, but concatenating string-literals is allowed like
    You clearly didn't finish reading my post:
    Quote Originally Posted by anduril462 View Post
    That sort of concatenation only works with literals, which are the things you put in double quotes " ". You need printf if you're mixing literals and variables.
    Code:
    puts( "\n" "foo" " " "bar" );
    or
    #define TOSTR(s) #s
    #define PRENL(s) "\n" TOSTR(s)
    puts( PRENL( name ) ); /* where name is not a variable, it will be quoted to a literal */
    Not sure if this is what you intended, but PRENL(name) will always produce "\nname" regardless of the contents of the name variable (or lack of variable). Try it and see for yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. puts array
    By meher81 in forum C Programming
    Replies: 21
    Last Post: 03-27-2010, 07:11 AM
  2. gets and puts does'nt work!!
    By behzad_shabani in forum C Programming
    Replies: 46
    Last Post: 06-16-2008, 10:18 AM
  3. puts( );
    By xddxogm3 in forum C Programming
    Replies: 9
    Last Post: 03-28-2005, 11:42 AM
  4. Puts() or Printf() ?
    By hern in forum C Programming
    Replies: 5
    Last Post: 06-25-2003, 06:09 AM
  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