Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the %n format specifier working normal for all scanf() statements but storing one less in the first one?

The %n format specifier,when used in a scanf() is expected to store the count of the number of characters of the format string already processed by the function into an argument of type int*.According to the definition:

The number of characters of the format string already processed is stored in the pointed location.

But in my program,it works so in all but the first scanf().In all scanf()s in my program,excluding the first one,it stores the count of the total number of characters entered from the console,including the newlines (Enter keys).But in the first scanf(), the count is one less than the total count of characters and newlines entered from the console.

Please explain this anomaly as it's really frustrating that I can't detect this simple bug.

#include <stdio.h>

int main ()
{
    int a,b,c,d,count;

    printf("First Trial\n");
    scanf("%d%d%d%d%n",&a,&b,&c,&d,&count);  //OUTPUT ANOMALY HERE
    printf("count=%d\n",count);

    printf("Second Trial\n");
    scanf("%d%n",&a,&count);
    printf("count=%d\n",count);

    printf("Third Trial\n");
    scanf("%d%d%n",&a,&b,&count);
    printf("count=%d\n",count);

    printf("Fourth Trial\n");
    scanf("%d%n%d",&a,&count,&b);
    printf("count=%d",count);
}

Sample Output

First Trial
253 
34  
4 
83
count=11

Second Trial
25
count=3

Third Trial
234 
38 
count=7

Fourth Trial 
3534
35
count=5

Why in the first trial we get "11" instead of "12?That's my doubt.

CRUCIAL EDIT

Another finding.If for the first scanf(),instead of using Enter keys(newlines) to separate the numbers entered, if I use space,lots of whitespace,then all those whitespace are counted by count as well.For example I get count=21.It means the newlines,spaces,everything is being considered.But why it is one less for first trial?

First Trial
25    35    38   98
count=21
like image 652
Thokchom Avatar asked May 18 '13 10:05

Thokchom


1 Answers

excluding the first one,it stores the count of the total number of characters entered from the console,including the newlines (Enter keys)

You are misinterpreting that. The scanfs do not consume the final newlines used to send the input to the programme, therefore the newline is left in the buffer to be consumed by the next scanf. All but the first scanf consume the newline from the previous input as the first character.

The first scanf consumes the eight digits plus the three newlines between the four numbers, that makes 11 characters.

The second consumes the newline from after the fourth number read in the first scanf, plus the two digits, makes 3 characters.

The third: newline, three digits, newline, two digits: 7 characters.

The fourth: newline, four digits: 5 characters. (then newline + 2 digits for b)

By the way, your quote

The number of characters of the format string already processed is stored in the pointed location.

is incorrect, it's not the number of characters of the format string, but the number of characters read from the input stream:

The corresponding argument shall be a pointer to signed integer into which is to be written the number of characters read from the input stream so far by this call to the fscanf function.

like image 53
Daniel Fischer Avatar answered Nov 15 '22 13:11

Daniel Fischer