Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does an asterisk in a scanf format specifier mean? [duplicate]

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?

like image 966
Joel Avatar asked Jan 19 '16 10:01

Joel


People also ask

What is format specifier in scanf?

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.

What is the meaning of the statement scanf \n s line );?

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.

What is the meaning of the statement scanf?

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.

Which format specifier we used in scanf () to scan a string?

The input for a %x format specifier is interpreted as a hexadecimal number. The scanf() function scans each input field character by character.


2 Answers

*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.

like image 98
Viktor Simkó Avatar answered Sep 29 '22 21:09

Viktor Simkó


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

like image 37
BeyelerStudios Avatar answered Sep 29 '22 20:09

BeyelerStudios