How does fgets work inside?

Well, this is the main question, but I seem to be pretty confused.

#include<stdio.h>
int main()
{
char a[100];
printf("Enter a string\n");
scanf("%s",a);
}

Basically, this is what I want to achieve. If I enter a line

James Bond

then I want this to be stored in array a. But the problem is that only the words of James are stored in the empty space between the letters. So how can I solve this problem.

UPDATE
After the answers below, I understand that fgets () would be a better choice. I want to know the inner workings of fgets, why it can store a string with a space where scanf cannot do the same.

+5
source share
5 answers

scanf . fgets, , :

fgets(a, 100, STDIN);

100 ( \n) .

gets, .

+1

scanf (, , ,...).

, " 5 42 -100" scanf("%d%d%d"), %d . %s.

, , %%, %[ %c (, , %n)

char input[] = " hello world";
char buf[100];
sscanf(input, "%8c", buf); /* buf contains " hello w" */
sscanf(input, "%8[^o]", buf); /* buf contains " hell" */

fgets , , .


Open Group Issue 7 -

+1

get (a)/fgets (a, sizeof (a), stdin) sscanf().

0

Try fgets () :

char a[100];
printf("Enter a string\n");
fgets(a, sizeof(a), STDIN);

To learn more about STDIN, check this out .

0
source

All Articles