- #1
TheSourceCode
- 14
- 0
I have the following chunk of C code that checks for errors. Works fine but I don't understand why it works.
The condition of the if statement is confusing me. From what I understand it is returning true if valid_input is false then terminates the program with an error code. What makes valid_input false when I type characters in it? Do all variables become false when there is data of the wrong type stored in them?
Code:
printf("\nEnter the number of students\n");
valid_input = scanf("%d", &snum);
if(!valid_input){
printf("\nInvalid number of students\n");
system("pause");
return 1;
}