Fevin Patel
Fevin Patel

Reputation: 5

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?

Upvotes: 0

Views: 399

Answers (2)

klutt
klutt

Reputation: 31296

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;

Upvotes: 1

ShahzadIftikhar
ShahzadIftikhar

Reputation: 523

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

Upvotes: 0

Related Questions