0

Is this code safe?

#include <stdio.h> int main(void) { char input[10]; scanf("%s", input), return 0; }

8th Jun 2022, 8:25 PM
Mrgoodatcode
Mrgoodatcode - avatar
3 Respostas
+ 3
yes it is safe but replace comma with semicolon at the end of scanf
8th Jun 2022, 11:28 PM
Aly Alsayed
Aly Alsayed - avatar
+ 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/
9th Jun 2022, 5:15 AM
Ipang
+ 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
9th Jun 2022, 9:26 AM
Aly Alsayed
Aly Alsayed - avatar