top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

C Program to Accept Paragraph using scanf?

+1 vote
457 views
C Program to Accept Paragraph using scanf?
posted May 22, 2014 by Atul Mishra

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

0 votes

Check the following program to see how newline character can be accepted in the scanf

#include<stdio.h>
void main()
{
  char para[100];
  printf("Enter Paragraph : ");
  scanf("%[^t]",para);
  printf("%s",para);
}

Enter Paragraph :

QueryHome is a 
Good source of
knowledge.

scanf("%[^t]",para); Here scanf will accept characters entered with spaces. It also accepts the Words , new line characters. [^t] represent that all characters are accepted except tab(t) , whenever tab is encountered then the process of accepting characters will be terminated.

Drawbacks :
Paragraph Size cannot be estimated at Compile Time and vulnerable to buffer overflows.

How to Specify Maximum Size to Avoid Overflow ?

// Accepts only 100 Characters
scanf("%100[^t]",para);
answer May 22, 2014 by Pardeep Kohli
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...