About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Wednesday, September 4, 2013

C Program to Accept Paragraph using scanf

Accept Paragraph using scanf in C
#include
void main()
{
char para[100];
printf("Enter Paragraph : ");
scanf("%[^t]",para);
printf("%s",para);
}

Output :[Press Tab to Stop Accepting Characters ]
Enter Paragraph : C Programming is very easy to understand
C
Language
is backbone of
C++
Language

How ?
scanf("%[^t]",para);
  1. Here scanf will accept Characters entered with spaces.
  2. It also accepts the Words , new line characters .
  3. [^t]  represent that all characters are accepted except tab(t) ,whenever t will encountered then the process of accepting characters will be terminated.
Drawbacks :
  1. Paragraph Size cannot be estimated at Compile Time
  2. It’s vulnerable to buffer overflows.
How to Specify Maximum Size to Avoid Overflow ?
//------------------------------------
// Accepts only 100 Characters
//------------------------------------
scanf("%100[^t]",para);

No comments: