In C, using scanf()
with the parameters, scanf("%d %*d", &a, &b)
acts differently. It enters value for just one variable not two!
Please explain this!
scanf("%d %*d", &a, &b);
If we want to skip any data from assigning into variables in the scanf() function in C programming language, then we can use the asterisk ( * ) symbol right after the percent ( % ) symbol in the scanf() function. So, we can do that by simply adding an asterisk * after the % .
For example, when reading an int : int i; scanf("%d", &i); the above cannot be used safely in case of an overflow. Even for the first case, reading a string is much more simpler to do with fgets rather than with scanf .
The “%d” in scanf allows the function to recognise user input as being of an integer data type, which matches the data type of our variable number. The ampersand (&) allows us to pass the address of variable number which is the place in memory where we store the information that scanf read.
The *
basically means the specifier is ignored (integer is read, but not assigned).
Quotation from man scanf:
* Suppresses assignment. The conversion that follows occurs as usual, but no pointer is used; the result of the conversion is simply discarded.
Asterisk (*) means that the value for format will be read but won't be written into variable. scanf
doesn't expect variable pointer in its parameter list for this value. You should write:
scanf("%d %*d",&a);
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