2
What is the purpose of the sscanf()
and the sprintf()
?
2
What is the purpose of the sscanf()
and the sprintf()
?
6
I mean in comparison to scanf()
and printf()
that are the most common that people have contact at the beginning of learning?
That one s
indicates that the operation will take place in a buffer of string previously existing, as opposed to the console that is normal in the most common input and output functions.
All operations of scan and printf take data from a stream or send to one. It can be the console, a file, or a string, that is, something direct in memory.
They all use a formatting engine to read or output data of various types adapting as needed by following rules set out in the API of these functions.
Documentation of scanf()
and of sprintf()
along with the others.
6
SSCANF
According to the book "The ANSI C Programming Language", by Brian W. Kernighan, the sscanf function, whose statement is
int sscanf(char *s, const char *format, ...);
is a function equivalent to scanf, whose declaration is
int scanf(const char *format, ...);
i.e., sscanf does the same as scanf, except that input characters are received from string s (see the sscanf statement above).
Example:
int idade, ano;
char *s = (char *) malloc(100 * sizeof(char));
s = "10 50";
sscanf(s, "%d%d", &idade, &ano);
printf("-> %s\n", s);
printf("-> idade: %d, ano: %d", idade, ano);
in the above code the string s has value "10 50" and I use this string as input in my reading, so the variable age and year get 10 and 50 respectively.
SPRINTF
According to the book "The ANSI C Programming Language", by Brian W. Kernighan, the sscanf function, whose statement is
int sprintf(char *s, const char *format, ...);
is a function equivalent to printf, whose declaration is
int printf(const char *format, ...);
that is, sprintf does the same as printf, except that the output is written in the string s with the addition of the null character ' 0' at the end of that string. The string s needs to be large enough to support the result.
Example:
int idade, ano;
char *s = (char *) malloc(100 * sizeof(char));
char *t = (char *) malloc(100 * sizeof(char));
s = "10 50";
sscanf(s, "%d%d", &idade, &ano);
printf("-> %s\n", s);
sprintf(t, "-> i: %d, a: %d", idade, ano);
printf("-> t: %s\n", t);
using the same previous example (with some additions at the end), I get the age and year values through sscanf, with this, I write the string "-> i: %d, a %d" on my string t, the first %d being the variable age, and the second, the variable year. If I print t, I will get "-> i: 10, to: 50".
Browser other questions tagged c scanf printf
You are not signed in. Login or sign up in order to post.
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).
– Maniero
John, I have already signaled the question to moderation; if you do not want this question no longer to be associated with your account, there is a way to dissociate them. Wait for moderation to resolve, but please stop vandalizing the question through edits.
– Woss