If the character you are trying to read does not match the conversion specification, the C> scanf ()> scanf function will leave the character unread in the stream.

I tried with ideone related to the code that reads the numerical value with scanf () in C, but I did not understand the behavior well.

http://ideone.com/PJ1dU2

#include <stdio.h>

int main(void) {
    int val;
    
    scanf("%d", &val);
    printf("val=%d\n", val);

    scanf("%d", &val);
    printf("val=%d\n", val);

    scanf("%d", &val);
    printf("val=%d\n", val);
    
	return 0;
}

stdin


A
3
1
4

result


val=-1218232755
val=-1218232755
val=-1218232755

I didn't understand why the number 3 could not be read.

http://f4.aaacafe.ne.jp/~pointc/log460.html

If the character you are trying to scan does not match the conversion specification, the scanf function will do so. Leaves the characters in the stream unreadable.

That's what it is. It wasn't an ideone bug either.

Recommended Posts

If the character you are trying to read does not match the conversion specification, the C> scanf ()> scanf function will leave the character unread in the stream.
If an exception occurs in the function, it will be transmitted to the caller 2
If an exception occurs in the function, it will be transmitted to the caller 1
Understand Python yield If you put yield in a function, it will change to a generator
What to do if Python does not switch from the System version in pyenv
Addresses an issue where an error does not occur even if the path does not pass when trying to read an image with cv2.imread ()