-1

Assume that i put ' 123abc' as input in C++ console application. if i get input as

int a; char ch;
scanf("%d", &a);
scanf("%ch", &ch);

i got 123 in a, 'a' in ch. Right?

Then, where does 4 blanks go? scanf just blow away blanks? Is there no way can i get that blanks after i get 123abc?

To sum it up my question,

  1. When i get int input with scanf, does scanf blow away blank in buffer? What does happen i execute that code?

  2. Can i get 4 blanks in ' 123abc' after i get '123' and 'a'?

Galik
  • 47,303
  • 4
  • 80
  • 117
Krsnik
  • 3
  • 1

2 Answers2

0

I recommend you to read this documentation about scanf.

Whitespace character: the function will read and ignore any whitespace characters encountered before the next non-whitespace character (whitespace characters include spaces, newline and tab characters -- see isspace). A single whitespace in the format string validates any quantity of whitespace characters extracted from the stream (including none).

and if you wont to read sentence you can use fscanf or getline EDIT This is how to do it -

scanf(" %[^\n]",str);

the anser is from this qustion here

yaodav
  • 1,126
  • 12
  • 34
0

As others pointed out, you really should read scanf documentation so you can see which specifier will be able to read whitespaces.

Regarding your first question:

When i get int input with scanf, does scanf blow away blank in buffer? What does happen i execute that code?

For numeric inputs, scanf will ignore any whitespace character.

When you execute scanf("%d", &a); it will go over the spaces (or any other whitespace character) until it finds a digit and start reading a decimal integer (as specified by %d).

Regarding your second question:

Can i get 4 blanks in ' 123abc' after i get '123' and 'a'?

I'm not sure what you mean here, you want to read a past value after having gone through the whole buffer? You won't be able to do this.

If you need the spaces, get them before reading anything else. Or read the whole buffer into your own buffer and deal with it. There are many ways to deal with this.

Here's an example:

#include <cstdio>

int main()
{
    {
        printf( "Reading into your own buffer:\n" );
        char my_buffer[128];

        scanf( "%127[^\n]%*c", my_buffer );  // the %*c is to throw away the trailing \n
        printf( "my_buffer: [%s]\n", my_buffer );
    }

    {
        printf( "Reading each part separately:\n" );
        char spaces[5];
        int number;
        char remaining_chars[4];

        scanf( "%4[ ]", spaces );
        scanf( "%d", &number );
        scanf( "%3s", remaining_chars );

        printf( "         spaces: [%s]\n", spaces );
        printf( "         number: [%d]\n", number );
        printf( "remaining_chars: [%s]\n", remaining_chars );
    }

    return 0;
}

And here is a sample run:

Reading into your own buffer:
    123abc
my_buffer: [    123abc]
Reading each part separately:
    123abc
         spaces: [    ]
         number: [123]
remaining_chars: [abc]
  • I'm not sure what you mean here, you want to read a past value after having gone through the whole buffer? You won't be able to do this. => Then is this correct? If scanf pass through whitespace, there is no way to read passed whitespace? Did I understand right? – Krsnik Oct 30 '19 at 10:45
  • I guess it's more accurate if phrased as: if `scanf` passes through any character be it ignoring or extracting it into a value (for example, converting the `"123"` string in the buffer to actual `123` integer number), than we should assume this data has been consumed and is no longer in the input buffer. So reading part of the buffer and then trying to go back isn't right. Unless, of course you use the first suggestion I made on my answer and read the whole buffer to a buffer you defined, than you can process it and still have access to the whole thing. – Leonardo Lourenço Oct 30 '19 at 11:41
  • I think i got it. Thx! – Krsnik Oct 30 '19 at 12:17