Incorrect usage of scanf()
// scanf(" %s
", ...
scanf("%s", ...
A white-space like '
'
after the "%s"
directs scanf()
to look for white-space until a non-white space follows. That non-white-space is put back into stdin
. This likely means OP had to enter 2 Enter for the 4th input.
Should always check the scanf()
return value to see if it is as expected.
// scanf("%d",&participante.nro_corredor);
if (scanf("%d",&participante.nro_corredor) != 1) Handle_Error();
Putting a space before "%s"
or "%d"
in scanf()
make no difference. These specifiers will consume optional white space even without a leading " "
.
Recommend to use fgets()
to read user input (which is evil) and then parse with sscanf()
, strtok()
, strtol()
, etc.
Note: except inside a scanset "%[...]"
, white-space like ' '
, ''
, '
'
, and others each perform the same: They consume any number of white-space. That '
'
in scanf(" %s
",...
does not scan for 1 '
'
. It scans for any number of any white-space. It will keep scanning white-space, including multiple Enters until EOF or a non-whitespace is entered. That non-white-space is then put back into stdin
as the next `char to be read.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…