So I stumbled across this code and I haven't been able to figure out what the purpose of it is, or how it works:
int word_count; scanf("%d%*c", &word_count);
My first thought was that %*d
was referencing a char
pointer or disallowing word_count
from taking char
variables.
Can someone please shed some light on this?
The format specifier is used during input and output. It is a way to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf(). Some examples are %c, %d, %f, etc.
In order to take a line as input, you can use scanf("%[^\n]%*c", s); where is defined as char s[MAX_LEN] where is the maximum size of . Here, [] is the scanset character. ^\n stands for taking input until a newline isn't encountered.
In the C programming language, scanf is a function that reads formatted data from stdin (i.e, the standard input stream, which is usually the keyboard, unless redirected) and then writes the results into the arguments given.
The input for a %x format specifier is interpreted as a hexadecimal number. The scanf() function scans each input field character by character.
*c
means, that a char will be read but won't be assigned, for example for the input "30a" it will assign 30 to word_count
, but 'a' will be ignored.
The *
in "%*c"
stands for assignment-suppressing character *
: If this option is present, the function does not assign the result of the conversion to any receiving argument.1 So the character will be read but not assigned to any variable.
Footnotes:
1. fscanf
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With