user2252464
user2252464

Reputation: 1

How to read a line from stdin

I am trying to read a text line from user but the fgets wont work right after the printf("Enter a string:"); the program terminates and doesn't give a chance to enter anything.

fgets(string, 100, stdin)

Upvotes: 0

Views: 192

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

Here is how you can do it:

char mystr[100];
if ( fgets (mystr, 100 , stdin) != NULL ) {
    puts (mystring);
}

You need to #include <stdio.h> in order for this to compile.

The first pointer must point to a memory block of enough size to fit the number of characters passed in the second pointer.

Upvotes: 1

Related Questions