Scanf regular expression partial summary

Regular expression

  Regular usage of scanf () function:

 scanf ( " % [^ \ n] ", str )  where  \ n  means that the saved string is terminated by a newline character, and the newline symbol is not stored. The purpose of this sentence is to allow the function to read in addition to the newline character The blank character (space, table), when normally input, scanf () cannot read blank characters such as spaces like gets () ;

 scanf ( " % * [^ \ n] " );   where  \ n  represents a newline, where * indicates that the content of the line is not assigned to any variable, and the content of the line is skipped directly.

Take the following code as an example for discussion

 1 # include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     char str1[100];
 6     char str2[100];
 7     char str3[100];
 8     char str4[100];
 9     
10     scanf("%[^\n]",str1);
11     scanf("%[^#]",str2);
12     scanf("%*[^\n]",str3);
13     scanf("%*[^#]",str4);
14 
15     puts(str1);
16     puts(str2);
17     puts(str3);
18     puts(str4);
19     
20     return 0;
21 } 

Typing:  where \ n  represents a newline

hello,   world!\n     //str1
hi,Alice.\n          //str2
here, Alice. # \ n // str2
Hello,Friday!\n   //str3
Hi,Morning!#\n  //str3

Output content:

hello,   world! // str1
hi,Alice.      // str2
Hey, Alice. // str2

discuss:

str1: ends with a newline character, and can read and assign space symbols to variables;

str2: end with # sign, you can read the newline character and assign the content to the variable;

str3: No output. End with a newline character, but do not assign a value to the variable;

str4: No output. End with # sign, the content is not assigned to the variable.

 

Guess you like

Origin www.cnblogs.com/Robin5/p/12670882.html