C language input string with spaces

Use scanf("%s",array); If you encounter a space, you will stop receiving the following characters. How can you receive a string with spaces?

1. With gets(), it can receive a string with spaces, and it will not end the input until the carriage return

char buf[80]={0}; 
gets(buf); //Can read spaces, press Enter to end the input

2. Using "%[^\n]" can also receive and input a string with spaces, until the carriage return ends the reception

char buf[10] = {0};

scanf("%[^\n]",buf); //Can read spaces, press Enter to end the input

Note: scanf_s("%s",buf,10); cannot accept string input with spaces, although its specified length is 10, it will automatically end the input even if there are no full 10 characters when a space is encountered.

 

[xxx], %[abc] means that the character combination includes a, b and c. If any characters other than these three characters are encountered, the reception will be stopped. %[^abc] means that the combination of characters is all characters except abc. As for whether the horizontal bar is used to specify a range of characters %[az], it depends on the compiler.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325931947&siteId=291194637