Bifalgorithm post · Computer science one · Tokenizing

How do sscanf() and sprintf() work with strings?

How do sscanf() and sprintf() work with strings? (Computer science one · Tokenizing) — "The sscanf() function differs in its first argument: the string that co…

Ideas in this flowchart

  • Formatted input and output functions process strings
  • sscanf() parses values from an input string
  • Its first argument is the string containing the value to parse
  • Its second and third arguments match scanf(): a format string and variables passed by reference
  • sprintf() is similar to printf(), but produces formatted text in a string
  • Instead of standard output, sprintf() places the result in a string
  • The example initializes x to 10, y to 3.14, and allocates space for s
  • sprintf() formats x and y and stores the result in s
  • The resulting string is "The value of x is 10, y = 3.140000."
Feed/

How do sscanf() and sprintf() work with strings?

No cover

Channel

Computer science one

1 page

"The sscanf() function differs in its first argument: the string that contains the value you want to parse. Otherwise, the second two arguments are as in scanf(): the format (as a string) and the variable(s) that the results should be stored in (passed by reference). Likewise, there is a companion sprintf() which is similar to the printf() function, but instead of printing to the standard output, it “prints” to the string. That is, the result is placed in a string.\n\nint x = 10;\n\ndouble y = 3.14;\n\nchar *s = (char *) malloc(sizeof(char) * 50);\n\nsprintf(s, \"The value of x is %d, y = %f.\", x, y);\n\n// s now contains \"The value of x is 10, y = 3.140000.\""

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.