Using Scanf to input many words?

Posted on 2003-11-16
Last Modified: 2008-02-01
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,
char name[15];
     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?

Question by:gopikrish
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 45

Expert Comment

ID: 9758568
Hi gopikrish,


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

LVL 45

Accepted Solution

sunnycoder earned 20 total points
ID: 9758577
> many words and till after I press enter key,how to do it
scanf ( "%[^\n]s", name );
fgets ( name, SIZE_OF_NAME, stdin );

both will read until an enter is pressed ... fgets is preferred

Author Comment

ID: 9758833
Ok thanks a lot sunnycoder :)

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem in finding output of a program 11 117
First character of input string truncated with scanf 3 117
C Programming - If Statement 8 84
delete-remove 14 116
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question