I am running into a bit of a problem that my sscanf line is always evaluating to true. I want it to evaluate to false if the fgets passes it a string with no digits…any advice/help here?
int main(void){
int choice;
char buffer[LINESIZE];
while (1){
printf("\nprompt");
if(fgets(buffer,LINESIZE, stdin)){
if(sscanf(buffer, "%d", &choice)){
/* do something*/
else
/*do something else*/
As said in the comments sscanf() returns the number of argument matched; the standard behaviour in C is that 0 is implicitly casted to FALSE and anything else is casted to true, so I guess 2 things can be happening either your string always contains an integer and it always reads something or your compiler is doing something weird and it’s casting 0 to true (very strange)
but in both cases you can fix this modifying the code as follows
you will get the wanted result and you will have a much more readable code (and correct since you don’t do strange casts)