Scanf in c and communication input buffer

I am trying to understand the relationship between scanf and the input buffer. I use scanf with the following format string:

int z1,z2; scanf("%d %d", &z1,&z2); 

And try to understand why I can enter as many spaces as possible (Enter, Blanks, Tabs) after entering a number like 54, and press enter.

As far as I understand, each key that I press is placed in the input buffer until we press Enter.

So, if I type 54 and press Enter, the input buffer contains 3 elements, two digits and a line break. Therefore, my buffer looks like [5] [4] [\ n]

Now scanf / formatstring is evaluated from left to right. Thus, the first% d corresponds to 54, 54 is stored in z1.

Due to a space in the format string, line break (\ n) caused by pressing the first input is "consumed".

So, after evaluating the first% d and the space (\ n), the buffer is empty again.

Now scanf is trying to evaluate the second (and last)% d in the format string. Since the buffer is now empty, scanf waits for further user input (user input = reading from stdin in my keyboard).

Thus, the sequence of states / actions of the buffer

buffer empty β†’ call scanf β†’ scanf blocks for user input β†’ user input: 54 Enter β†’ buffer contains: [5] [4] [\ n] -> evaluate the first buffer% d β†’ contains [\ n] -> space estimate β†’ buffer empty -> scanf blocks for user input (due to evaluation of the second and last% d) -> ...

I got it right? (sorry, english is not my native language)

considers

+5
source share
2 answers

As far as I understand, each key that I press is placed in the input buffer until we press Enter.

Right. Pressing Enter allows you to dump data into stdin (standard input stream). Note that it also sends \n to stdin .

So, if I type 54 and press Enter, the input buffer contains 3 elements, two digits and a line break. Therefore, my buffer looks like [5] [4] [\ n]

Yes.

Now scanf / formatstring is evaluated from left to right. Thus, the first% d corresponds to 54, 54 is stored in z1.

Right

Due to a space in the format string, line break (\ n) caused by pressing the first input is "consumed".

Right.

So, after evaluating the first% d and the space (\ n), the buffer is empty again.

Yes.

Now scanf is trying to evaluate the second (and last)% d in the format string

Not really.

The space between the two %d is a space character and whitespace in the scanf format string tells scanf to scan and discard all whitespace characters, if any, to the first character without spaces . This can be seen in n1570, draft C11 standard committee:

7.21.6.2 fscanf function

[...]

  1. A directive consisting of white space character (s) is executed by reading the input up to the first character of a non-white space (which remains unread), or until more characters are read. The directive will never work.

This means that execution is still in the space between %d since it has not yet met the character without spaces.

Since the buffer is now empty, scanf waits for further user input (user input = reading from stdin in my keyboard).

Yes.

So,

buffer empty β†’ call scanf β†’ scanf blocks for user input β†’ user input: 54 Enter β†’ buffer contains: [5] [4] [\ n] β†’ evaluate the first% d - β†’ buffer contains [\ n] β†’ space evaluation β†’ the buffer is empty β†’ scanf blocks for user input (due to evaluation of the second and last% d) β†’ ...

it should be

"The buffer is empty β†’ call scanf β†’ scanf blocks for user input β†’ user input: 54\n β†’ buffer contains: 54\n β†’ first score %d - β†’ buffer contains \n β†’ space estimate β†’ empty buffer β†’ scanf blocks for user input ( due to gap rating) β†’ ... "


Note that scanf will behave the same when there are many whitespace characters or spaces between %d characters (up to %d ), since %d already skips leading whitespace characters. In fact, the only format specifiers for which space characters are significant are %c , %[ and %n , as shown in n1570:

7.21.6.2 fscanf function

[...]

  1. Entering space characters (as indicated by the isspace function) is omitted if the specification does not include the [ , c or n specifier. 284
+3
source

To a large extent.

Scanf reads the input buffer (stdin).

In the cmd.exe input window, press the enter key to discard the input you entered into the input buffer, cue, so that your first variable is filled.

Then it asks again to populate the second variable.

+1
source

Source: https://habr.com/ru/post/1234270/


All Articles