int a;
scanf("%d",&a);
How can I ensure the code doesn't work if a non-digit character is given as input to the scanf() statement? [I need a solution that doesn't make me change the data type to char]
int a;
scanf("%d",&a);
How can I ensure the code doesn't work if a non-digit character is given as input to the scanf() statement? [I need a solution that doesn't make me change the data type to char]
If the first character is not a digit, then %d will fail to match, and a will not be assigned. The return value of scanf tells you how many items were assigned. If it's one, then clearly it was at least partially a valid number. If it's zero, that means it couldn't be parsed as a number, and you may want to signal an error.