c - 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 entered : %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 %d",y); }
here 2 statements , in first statement want ask why printf()
don't give error or warning?
printf("number of character entered : %d",printf("you entered age : %d\n",i,scanf("%d",&i),printf("enter age : "))-19);
second statement when executing asking input 2 times, first time after
"enter number(y) : "
after entering number ask input , don't know why.
and value of y
value entered first time what's matter behind second input ?
scanf("%d ",&y,printf("enter number(y) : "));
for first printf()
statement, quoting c11
standard, chapter 7.21.6.1, fprintf()
if format exhausted while arguments remain, excess arguments evaluated (as always) otherwise ignored.
so, there no error generated.
in case of scanf()
, issue with
scanf("%d ",&y,printf("enter number(y) : ")); ^ |
the trailing white-space after format specifier. tells ignore any number of trailing white-space after first input matching conversion specifier. upon encountering non-white-space character, complete scanning.
quoting chapter §7.21.6.2
a directive composed of white-space character(s) executed reading input first non-white-space character (which remains unread),.....
solution: remove trailing white-space after conversion specifier.
scanf("%d",&y,printf("enter number(y) : "));
fwiw, in case of having excess argument conversion specifiers in format string defined behavour, per c11
, chapter 7.21.6.2
if format exhausted while arguments remain, excess arguments evaluated (as always) otherwise ignored.
this horrible way write code.
Comments
Post a Comment