Take the 2-minute tour ×

I make some research in order to look for the sscanf() source code . But I could not find the answer to my question.

when we use sscanf() in this way:

char str[50] = "5,10,15";
int x;

Does sscanf() support "recursive" buffer str ?

share|improve this question
Reading from, and writing to the same memory area at the same time is usually not a good idea. –  Joachim Pileborg Nov 27 '12 at 10:32
I know this. but it could be supported by the sscanf function. and that why I m looking for. May be the scanf does not have problem with that. and this will allow me to optimize my code. I will not have to add another buffer and make copy ... –  MOHAMED Nov 27 '12 at 10:33
+1 Different question –  Alberto Bonsanto Nov 27 '12 at 10:36
BTW I tested it in a small program and I did not get any problem in the execution. But I want a confirmation from some one who knows the code –  MOHAMED Nov 27 '12 at 10:39
pubs.opengroup.org/onlinepubs/9699919799/functions/fscanf.html has a restrict on the source string. Passing another pointer to str in the argument list would, I think, violate the restrict promise, but I'm not entirely sure about that. –  Mat Nov 27 '12 at 10:40

3 Answers 3

up vote 3 down vote accepted

It doesn't break from self modifying buffer. But to make it (tail)recursive, you'd have to read to the end of the string.
The code fragment:

char str[]="5,10,15";
int a[10]={0},x = 0; 
while (sscanf(str,"%d,%s",a+ x++,str)>1);

reads all the integers.

Since this is not really recursive and the string to be read doesn't overwrite the asciiz in the string, I believe this is "safe" in the meaning: only try this at home.

share|improve this answer
Neat trick. Clearly less efficient than using %n and looping with increasing offset into str, which is probably what the question demands, but it's a good idea. You'd have to implement sscanf in a fairly silly way for this not to work, I feel. –  Nicholas Wilson Nov 27 '12 at 10:52

scanf family functions read the format specifiers and do the conversion one-by-one. In your example code:

char str[50] = "5,10,15";
int x;

It may work because it reads in an integer first and another c-string. No problem here as original str is over-written with the new value.

But consider the following:

char str[50] = "5 10 15";
int x;
sscanf(str,"%s %d",str, &x);

It first reads 5 from the original value and overwrites str and subsequent format specifier %d will have nothing to read from str as the end of str has been reached due to the nul-termination for the previous %s read.

It's just a counter example to show it's a bad idea and won't work. So you can say this is going to invoke undefined behaviour at some point or cause other problems.

share|improve this answer
Agreed. And even if correctly used, a recursive sscanf() is likely to trigger all the alarms for the next programmer who maintains your code, making them duplicate this non-trivial deduction. Respect your colleagues' time. Keep your code readable and don't make them think! –  deStrangis Nov 27 '12 at 11:01

Generally I would advise against this. If it works once it doesn't mean, that it will in all corner cases. To be really sure, you'd have to check sources for the very implementation you are using. If you want it to be portable, forget about it right away, unless you see it written in the libc specs as a guaranteed behaviour. Use strchr() to find the next delimiter and update the string pointer to point at the next character.

share|improve this answer

Your Answer


