+ 1

How to read a paragraph with white spaces using scanf function in c language.?

1st Jul 2019, 11:51 AM
Gaurav Rawat
Gaurav Rawat - avatar
3 Answers
+ 2
Try something like : char str[100]; scanf ("%[^\n]%*c", str); -- =================== I think that fgets function is better to use for your purpose....
1st Jul 2019, 12:38 PM
Prokopios Poulimenos
Prokopios Poulimenos - avatar
+ 2
Please Can you explain me what have you written in scanf function ?i dont understand.
1st Jul 2019, 4:12 PM
Gaurav Rawat
Gaurav Rawat - avatar
+ 2
The expression "%[^\n]%*c" means that all the characters entered as the input, including the spaces, until we hit the enter button are stored in the variable str To be more specific, [^\n] is regular expression that will exclude \n i.e in other words scanf will read all char until you hit enter button. %*c , * is used to indicate don’t assign value to str until you hit enter or scanf encounters ‘\n’.
2nd Jul 2019, 5:56 AM
Prokopios Poulimenos
Prokopios Poulimenos - avatar