...
In this noncompliant code example, control reaches the end of the checkpass()
function when the two strings passed to strcmp()
are not equal. This event leads to undefined behavior, and various compilers generate code equivalent to the checkpass()
function, returning various values when no return
statement is executed in checkpass()
.
...
This compliant solution ensures that the checkpass()
function always returns a value.:
Code Block | ||||
---|---|---|---|---|
| ||||
int checkpass(char *password) { if (strcmp(password, "pass") == 0) { return 1; } return 0; } /* ... */ if (checkpass(userinput)) { printf("Success!\n"); } |
...