C Program to Accept Paragraph using scanf: I f you think that scanf does not accept paragraph then your are wrong,because here we learn a way from which we can insert paragraph with space.
C Program to Accept Paragraph using scanf
#include<stdio.h>
int main() {
char pgraph[400];
printf("Enter Paragraph : ");
scanf("%[^\t]s", pgraph);
printf("Accepted Paragraph : %s", pgraph);
return 0;
}
Note: Press Tab to Stop Accepting Characters
Output :
Enter Paragraph :Geek Coderz is a blog which covers the stuff related to progr amming languages and tutorials.
Accepted Paragraph : Geek Coderz is a blog which covers the stuff related to pr ogramming languages and tutorials.
Explanation :
scanf("%[^\t]s", pgraph);
- Here scanf will accept Characters entered with spaces.
- It also accepts the Words , new line characters .
- %[^\t]s represent that all characters are accepted except tab(t) , whenever t will encountered then the process of accepting characters will be terminated.
Drawbacks :
- Paragraph Size cannot be estimated at Compile Time
- It’s vulnerable to buffer overflows.
How to Specify Maximum Size to Avoid Overflow ?
// Accept only 100 character
scanf("%10[^\t]s", pgraph);