简体   繁体   中英

Why printf() don't gives any error and issue with scanf()

Code:

int main(int argc,char **argv)
{
  int y,i;
  printf("Number of character you entered is : %d",printf("you entered age : %d\n",i,scanf("%d",&i),printf("Enter age : "))-19);
  printf("\n\n");
  scanf("%d ",&y,printf("Enter number(y) : "));
  printf("Value of y is %d",y);
}

Here are two statements , In first statement I want to ask why printf() don't give any error or warning?

printf("Number of character you entered is : %d",printf("you entered age : %d\n",i,scanf("%d",&i),printf("Enter age : "))-19);

Second statement when executing is asking for input two times, first time after

"Enter number(y) : "

after entering number it ask for another input , don't know why.

And value of y is the value you entered first time so what's the matter behind second input ?

scanf("%d ",&y,printf("Enter number(y) : "));

For the first printf() statement, quoting C11 standard, chapter 7.21.6.1, fprintf()

If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored.

So, there is no error generated.

in case of scanf() , the issue is with

 scanf("%d ",&y,printf("Enter number(y) : "));
          ^
          |

the trailing white-space after the format specifier. Basically it tells to ignore any number of trailing white-space after the first input matching the conversion specifier. Upon encountering the a non-white-space character, it would actually complete scanning .

Quoting chapter §7.21.6.2

A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread),.....

solution: remove the trailing white-space after the conversion specifier.

scanf("%d",&y,printf("Enter number(y) : "));

FWIW, even in case of having the excess argument than the conversion specifiers in the format string is defined behavour, as per C11 , chapter 7.21.6.2

If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored.

this is a horrible way to write a code.

It doesn't ask input "two times" rather scanf() is waiting for you to input a non-space whitespace character to terminate the input reading because of the extra space you have in the format string: "%d " .

A whitespace directive in scanf() will read and discard any number of whitespace characters. Hence, you are forced to input a non-whitespace character:

scanf manual states:

· A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space, including none, in the input.

In any case, you are passing too many arguments to scanf() than the format specifiers you pass and this is a terrible way of passing printing and scanning inputs a single statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM