A question about the return value of scanf in C language?

topic 1

< H1 > include < stdio.h > < / H1 >

int main (void) {

int a;
printf("plese \n");
scanf("%d", &a);
printf("number is %d\n", a);
return 0;

}

case 1: enter a number and return the correct result

case 2: enter f56 to return the garbled code (because the integer cannot be read, a random number is returned, is that correct)

topic 2

< H1 > include < stdio.h > < / H1 >

int main (void) {

 char b[20], c[20];
 int a;
 scanf("%d", &a);
 scanf("%s", b);
 scanf("%s", c);
 printf("a==%d, b==%s, c==%s.\n", a, b, c);
 return 0;

}
if I enter f567 like this,
Why
astat0; bounded f5, clocked 6percent?

Thank you for the answer

C
Jul.14,2021

  1. because you entered an invalid value, a will not be assigned, leaving the initial value, so it looks like a random number.
  2. an is invalid, the initial value is retained, and the content in the input stream remains the same, so f5 is assigned to bjournal 6 and assigned to c.

(I'm not sure why an is 0 instead of a random value in the second program. According to reason, it should be a random value.)


The

scanf () function is a formatting input function that reads input information from a standard input device (keyboard).
if your input data does not match the required data format, a will not be assigned, and the return value will be 0, not a random number. Return value of
clipboard.png
scanf ():

  1. positive integer, number of data read correctly
  2. 0, the input data does not match the required data format, that is, the number of data read correctly is 0.
  3. the constant defined by EOF, in stdio.h. The value is generally-1, which is input CTRL+Z for Windows.

in the second question, the input of the first f5 does not meet the format requirements, is not read, and the return value is 0, so the output 0focus f5 meets the requirements of b format and is assigned to b and output f5.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b40c16-2c52e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b40c16-2c52e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?