简体   繁体   中英

How to take Line input in C language with whitespace

I am going through one small code where I need to take whole line as input through scanf function, and I think there is one format specifier "[^\n%*c]" can someone please tell me syntax for it and how it works?

The best way is probably to use fgets .

char str[20];
fgets(str, 20, stdin);
printf("%s", str);

No reason to use scanf here. Just remember that fgets also puts the newline character in the buffer. It can be removed like this:

str[strcspn(str, "\n")] = 0;
#include <stdio.h>
int main()
{
   char str[20];
   scanf("%19[^\n]%*c", str);
   printf("%s\n", str);
 
   return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM