foreword
%*s is part of the format string used to skip a string during input without storing it into a variable. This is useful when dealing with input where certain parts need to be skipped.
main body
Specifically, %*s works as follows:
%*s: Indicates to ignore a string. * is used to specify an optional field width, but in this case the field width has no real effect.
s: The format specifier indicates to read a string.
Here is an example showing how to use %*s to skip strings in the input:
#include <stdio.h>
int main() {
char name[100];
char city[100];
printf("Enter your name: ");
scanf("%*s"); // 跳过输入的字符串,不存储到name变量
printf("Enter your city: ");
scanf("%s", city); // 将输入存储到city变量
printf("City: %s\n", city);
return 0;
}
In this example, %*s is used to skip the user-entered name, and %s is used to read the user-entered city. Therefore, the name entered by the user will be ignored, only the city will be read and printed.
Precautions
The %*s format specifier cannot actually be used directly to skip whitespace. It is used to skip strings regardless of whether there are spaces in the string. When using %*s, it ignores the entire string, including all characters in it, including spaces.