ShadyBears
ShadyBears

Reputation: 4185

How do I read in white spaces with scanf?

It completely stops reading in code after it sees a space. How do I change my code so it reads in white space

char line[300];

printf("Enter a string to be checked: ");
scanf("%s", line);

the string I'm trying to input via redirection is:

( ( a a ) < > [ [ [ { [ x ] ]]] <>)

Upvotes: 0

Views: 300

Answers (4)

hbdavehb
hbdavehb

Reputation: 220

you should use fgets(line, size, stdin); as previously posted. you should never use gets(), as it expects a the same input size every time. Compilers, at least gcc, will warn you not to use it.

Upvotes: 2

Akshay Joy
Akshay Joy

Reputation: 1765

Please check the code..

  printf("Enter a string to be checked: ");
    gets( line);
    puts(line);

Output:-

Enter a string to be checked: ( ( a a ) < > [ [ [ { [ x ] ]]] <>)
( ( a a ) < > [ [ [ { [ x ] ]]] <>)
Press any key to continue . . .

Upvotes: -1

someuser
someuser

Reputation: 189

You can use fgets intsead of scanf. For example:

    fgets(line, 1024, stdin);

Upvotes: 2

Ian McMahon
Ian McMahon

Reputation: 1700

%s Matches a sequence of non-white-space characters; the next pointer must be a pointer to char, and the array must be large enough to accept all the sequence and the terminating NUL character. The input string stops at white space or at the maximum field width, whichever occurs first.

Need to choose a different format string. It's not really clear from your input string what exactly you're trying to accomplish.

Upvotes: 0

Related Questions