Check the return of scanf()
. When you type in characters, the correspond result will occur.
if (1 == scanf("%d", &number)) {
printf("The result is:%d", number);
}
else {
printf("Invalid data entered. `number` not changed\n");
}
Note: Code's int number;
is not initialized, so its value could be any int
. With invalid input, number
was not changed and code printed the uninitialized number
which just happeded to have the value of "1986895412". It may differ tomorrow.
Note: leading space in " %d"
is not needed as %d
itself consume leading whitespace.