DEV Community

Mujahida Joynab
Mujahida Joynab

Posted on

How to take string input in c

Method 01:
To take input string of a word we use

char str[30] ;
scanf("%s",str) ;
printf("%s",str) ;
Enter fullscreen mode Exit fullscreen mode

This function takes input of a word but not a line .
Example -
Input- I love dates.
Outpu - I
Method 1.1 :
To take a input of a line until a new line we use -

char str[30] ;
scanf("%[^\n]s",str) ;
printf("%s",str) ;

Enter fullscreen mode Exit fullscreen mode

Input- I love dates.
Output- I love dates.
Method 1.2 :
We can also use -

fgets(str,sizeof(str),stdin); // To take input
fputs(str,stdout)  ;//To get output
Enter fullscreen mode Exit fullscreen mode

Input - I love dates
Output - I love dates

Here puts function works like print function .

Top comments (0)