Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

Using Scanf to input many words?

In C ,I want to scanf a line consisting of many words and till after I press enter key,how to do it?
 For eg,
 main()
{
char name[15];
scanf("%15c",name);
}
     This would be fine but the problem is I need to enter 15 characters.So what I want is as soon as I press enter I want the scanf to scan the words I entered.
In C++ its conveniently done using cin.get() function but how to do in C?
Thanks.


0
gopikrish
Asked:
gopikrish
  • 2
1 Solution
 
sunnycoderCommented:
Hi gopikrish,

try
scanf("%s",name);

but beware, if number of chars entered is more than your allocated space can hold, program will seg fault

Cheers!
Sunny:o)
0
 
sunnycoderCommented:
gopikrish,
> many words and till after I press enter key,how to do it
scanf ( "%[^\n]s", name );
or
fgets ( name, SIZE_OF_NAME, stdin );

both will read until an enter is pressed ... fgets is preferred
0
 
gopikrishAuthor Commented:
Ok thanks a lot sunnycoder :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now