Doubt in stdin flow of scanf() function

Asked

Viewed 33 times

0

Greetings to all. I am a beginner in C and I came across the following question regarding data entry;

int number1,number2;

printf("Digite o numero : ");
scanf("%d",&number1);
printf("Digite outro numero : ");
scanf("%d",&number2);

In this code if the user enters whole numbers twice will work normally. However, if a character other than an integer, in scanf("%d",&number1); , for example, any other incoming call as in scanf("%d",&number2); and all other calls scanf() in the code enter random values, apparently.

A doorway scanf("%c",&number1); works partially, that is, apparently the conversion of the input to character was performed, even if the value is not the same as the one we typed in integer value and skips the scanf() next.

Here in the community I found an answer very close to what I was looking for but not this fact in particular with the function scanf(). Parameters of Scanf Function( )

So my question is, why the insertion of a char on the call scanf("%d",&number1); does not cause error and for the execution of the program?

And since it does not cause error, apparently, why does not occur the call to the input of number2 and subsequent calls from scanf() for insertion of values, including by inserting such unknown values, totally ignoring the requested commands?

Thanks in advance.

  • Thanks for the return, I will even adopt this good practice, but the reply does not answer my question. I do not refer to a way of filtering the data entry but rather how the conversion of the reading by the keyboard scanf() for cause the insertion of such apparently random characters all other requests of the code and preventing the other variables from being read.

  • It’s not the scanf that inserts the random data. The numbers are random in their example because they were not initialized, as they were not initialized they have a random value that is equivalent to the state of memory at the time the variables were allocated in the stack. So when the scanf fails, it does absolutely nothing and leaves the variables unchanged. If you don’t understand much of what I said, take a test: replace int number1,number2; for int number1 = 42; int number2 = 21; and then see what values these variables have after scanf.

  • As for the error question. The answer given by the community answers your question. And, finally, why the scanf can’t read the number2 if the number1 failed is that as it failed the first time, it did not clear the input buffer(stdin), so when it will try to read the number2, it finds the character you typed... and fails again. Again, do a test. After the line scanf("%d",&number1);, insert scanf("%*s");, run your program and you will see that the number2 is read.

  • Thank you so much I did the tests here and it worked exactly as Voce said. It was exactly what I wanted to know.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.