0
Is this code safe?
#include <stdio.h> int main(void) { char input[10]; scanf("%s", input), return 0; }
3 Réponses
+ 3
yes it is safe but replace comma with semicolon at the end of scanf
+ 2
To be safer, you can specify a limit for number of characters to be read within the conversion specifier.
Specify a value of <buffer-size - 1>. So in this case your buffer <input> size is 10 characters. Thus you specify 9 for the limit of characters to be read in.
scanf( "%9s", input );
Why bother setting the read buffer limit? because something can go wrong if a program attempts to write more than what it was allowed for, or supposed to.
More about scanf() function
http://www.cplusplus.com/reference/cstdio/scanf/
+ 1
in addition this code will read a single word from user so if u add spaces after words, the program will only read the first word