Thread: scanf and sprintf

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    4

    scanf and sprintf

    Code:
    // enum { SZ = 250 } ;
    
       char cstr[SZ] ;// prepare format string to read at most SZ-1 characters
       char format[16] ;
       sprintf( format, "%%%ds", SZ-1 ) ; // ie. "%249s" if SZ == 250
    
       scanf( format, cstr ) ;
    im not too sure what sprintf do here, "%%%ds" and i know this format the argument but i search it means %4s? but im not sure how can i understand it,
    my guess is it formatted string of max 250 to array format, but why char format[16]?
    and in scanf(format, cstr) what the role of scanf? is cstr argument?
    thankyou!
    Last edited by yuuki; 10-22-2017 at 01:50 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    sprintf does the same as printf, but outputs to a string instead of the console. "%%" simply outputs a "%", done this way because it's a format character by itself. Then the "format" string is passed to scanf to tell it how/what to read.

    The idea behind this piece of code probably is to be able to easily change the string's length. It's normally done when the length is only known at run-time.
    Last edited by GReaper; 10-22-2017 at 02:18 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    4
    Quote Originally Posted by GReaper View Post
    sprintf does the same as printf, but outputs to a string instead of the console. "%%" simply outputs a "%", done this way because it's a format character by itself. Then the "format" string is passed to scanf to tell it how/what to read.

    The idea behind this piece of code probably is to be able to easily change the string's length. It's normally done when the length is only known at run-time.

    thanks i understand but not to sure about why format[16]?, if %250s means 250 char,why the array that it is stored to only consist of 16 char? if the user input larger than 16 char? i mean to change the string length, how should i decide the array?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    The "format" string doesn't need to be longer than that because it holds the format that scanf will use down the line, not the user's input.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Oct 2017
    Posts
    4
    Quote Originally Posted by GReaper View Post
    The "format" string doesn't need to be longer than that because it holds the format that scanf will use down the line, not the user's input.
    thanks but still confuse, the format string is 16 and store %250s, how can i know that %250s is 5 character long? and how can i convert 16 to %10000000000000s?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. use of sprintf
    By mafalda in forum C Programming
    Replies: 1
    Last Post: 01-03-2016, 08:35 AM
  2. UTF-16 and sprintf
    By Algar in forum C Programming
    Replies: 8
    Last Post: 09-18-2013, 10:14 AM
  3. Replies: 5
    Last Post: 02-12-2010, 08:02 PM
  4. sprintf in C#?
    By SuperNewbie in forum C# Programming
    Replies: 3
    Last Post: 07-09-2002, 04:35 PM
  5. Sprintf
    By Paninaro in forum Game Programming
    Replies: 2
    Last Post: 06-28-2002, 05:36 PM

Tags for this Thread