scanf in c and relation input buffer -


i try understand relation between scanf , input buffer. use scanf following format string:

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

and try understand why can enter many possible whitespace (enter, blanks, tabs) after type in number 54 , press enter.

as far understand every key press put in input buffer until press enter.

so if type in 54 , press enter input buffer contains 3 elements, 2 digits , line break. buffer looks [5][4][\n]

now scanf/formatstring evaluated left right. first %d matches 54, 54 stored in z1.

because of whitespace in format string line break (\n) caused pressing first enter "consumed".

so after evaluation of first %d , whitespace (\n) buffer empty again.

now scanf tries evaluate second (and last) %d in format string. because buffer empty scanf waits further user input (user input = reads stdin in case keyboard).

so buffer state/action sequence is

buffer empty -> call of scanf -> scanf blocks user input --> user input is: 54 enter --> buffer contains: [5][4][\n] -> evaluation of first %d --> buffer contains [\n] -> evaluation of whitespace --> buffer empty --> scanf blocks user input (because of evaluation of second , last %d) --> ...

did understand correct? (sorry, english not native language)

regards

as far understand every key press put in input buffer until press enter.

correct. pressing enter flushes data stdin(standard input stream). note sends \n stdin.

so if type in 54 , press enter input buffer contains 3 elements, 2 digits , line break. buffer looks [5][4][\n]

yes.

now scanf/formatstring evaluated left right. first %d matches 54, 54 stored in z1.

right.

because of whitespace in format string line break (\n) caused pressing first enter "consumed".

correct.

so after evaluation of first %d , whitespace (\n) buffer empty again.

yes.

now scanf tries evaluate second (and last) %d in format string

not quite.

the space between 2 %d whitespace character , whitespace characters in format string of scanf instructs scanf scan , discard whitespace characters, if any, until first non-whitespace character. can seen in n1570, committee draft of c11 standard:

7.21.6.2 fscanf function

[...]

  1. a directive composed of white-space character(s) executed reading input first non-white-space character (which remains unread), orn until no more characters can read. directive never fails.

this means execution still in space between %ds hasn't encountered non-whitespace character yet.

because buffer empty scanf waits further user input (user input = reads stdin in case keyboard).

yes.

so,

buffer empty -> call of scanf -> scanf blocks user input --> user input is: 54 enter --> buffer contains: [5][4][\n] -> evaluation of first %d --> buffer contains [\n] -> evaluation of whitespace --> buffer empty --> scanf blocks user input (because of evaluation of second , last %d) --> ...

should be

"buffer empty -> call of scanf -> scanf blocks user input --> user input is: 54\n --> buffer contains: 54\n -> evaluation of first %d --> buffer contains \n -> evaluation of whitespace --> buffer empty --> scanf blocks user input (because of evaluation of whitespace) --> ..."


note scanf behave same way when there many whitespace characters or no whitespace characters between %ds (before %d) %d skips leading whitespace characters. in fact, format specifiers whom whitespace characters significant %c, %[ , %n seen in n1570:

7.21.6.2 fscanf function

[...]

  1. input white-space characters (as specified isspace function) skipped, unless specification includes [, c, or n specifier. 284

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -