C language learning notes——scanf(%[^n]%c)

C language learning notes——scanf(%[^n]%c)

scanf and format specifiers

参考【scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s - cppreference.com】

What we need to understand is [%*[^\n]% c], now break it down into two parts [% [^\n]] [%*c].

Each conversion specifier match begins with a percent sign [%].

The asterisk [*] after % means [assignment suppression character], which means no parameters are required to accept the matching result of the scanf function.

for example:

#include <stdio.h>

int main(void)
{
    
    
    int number = 0;

    printf("input a number: ");
    scanf("%d", &number);

    printf("number is [%d]\n", number);

    return 0;
}

The user is required to enter a number and assign it to the variable number.
The running results are as follows:
Insert image description here

If I add * after % to become %*d, it will not be assigned to number.

#include <stdio.h>

int main(void)
{
    
    
    int number = 0;

    printf("input a number: ");
    scanf("%*d", &number);  // add *

    printf("number is [%d]\n", number);

    return 0;
}

The running results are as follows:

Insert image description hereWe will find that the number entered by the user cannot be assigned to the variable number. [% c] means matching a character (%c is a matching character), but not assigning it to a variable. Then what does [% [^\n]] mean? \n is the newline character. The square brackets [ ] represent sets. Yes, they are sets in your first lesson in high school mathematics. If the first character of the set is ^, it matches characters that are not in the set. (Complementary set learned in high school) The content in the set can also be a range such as: [1-9], [az]

#include <stdio.h>

int main(void)
{
    
    
    char str[100];

    printf("input a number: ");
    scanf("%[a-zA-Z]", str);

    printf("[%s]\n", str);

    return 0;
}

The running results are as follows:
Insert image description here

To sum up, [%*[^\n]] matches all characters before the newline character, but does not assign a value.

You can also add numbers after % to indicate how many characters are matched.

#include <stdio.h>

int main(void)
{
    
    
    char str[100];

    printf("input a number: ");
    scanf("%5[a-zA-Z]", str); // note 5

    printf("[%s]\n", str);

    return 0;
}

The running results are as follows:
Insert image description here

Only matched 5 characters

If %*[^\n] cannot match characters, the following %*c will not match either. So we must absorb the newline characters and separate these two sentences.

scanf("%*[^\n]");
scanf("%*c");

Summarize:

The wildcard character % [^\n] means to skip all characters until the newline character.
^for (i=0;i<2;i++) fscanf(fin,"%
[^\n]% c"); Skip 2 lines.
%
means "skip"
[^\n]. The delimiter of the string is "\n". You can write the delimiter table in the square brackets
%*[^\n] to skip all strings before \n.
%*c is a newline character that "skips" the end of a line.

Edited on: December 8, 2022

Guess you like

Origin blog.csdn.net/weixin_49796643/article/details/128241206