C Program to Accept Paragraph using scanf
Accept Paragraph using scanf in C
1 2 3 4 5 6 7 8 9 10 11 12 | #include<stdio.h> int main() { char para[100]; printf("Enter Paragraph : "); scanf("%[^\t]s", para); printf("Accepted Paragraph : %s", para); return 0; } |
Output :[Press Tab to Stop Accepting Characters ]
1 2 3 4 | Enter Paragraph : Pritesh Taral is author of c4learn.com Accepted Paragraph : Pritesh Taral is author of c4learn.com |
How ?
1 | scanf("%[^\t]s", para); |
- 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 ?
1 2 3 4 | //------------------------------------ // Accepts only 100 Characters //------------------------------------ scanf("%10[^\t]s", para); |
Output :
1 2 | Enter Paragraph : Pritesh Taral is author of C4learn Accepted Paragraph : Pritesh Ta |
Download PDF :