Reputation: 429
Is there some difference between
scanf("%s", c);
and
scanf(" %s", c);
Like can it affect my program in any way? Thanks in advance.
Upvotes: 0
Views: 81
Reputation: 144520
The scanf
%s
conversion specifier skips leading whitespace characters and parses a word up to and not including subsequent whitespace.
Adding a space in front of the %s
has no effect, it is fully redundant.
The same holds for %d
and %f
but not %c
or %[
.
Note also that the %s
and %[
specifiers are risky since you do not provide scanf
any limit for the number of characters to store into the destination. This might be OK for sscanf()
as the conversion is implicitly limited by contents of the source string, but must be avoided for scanf
and fscanf
.
You can provide a numeric argument between the %
and the s
for the maximum number of chars to store before the null terminator:
char buffer[100];
if (scanf("%99s", buffer) == 1) {
/* a word was parsed correctly into buffer */
}
Upvotes: 8