*(yes+0) == 'y'
*(yes+1) == 'e'
*(yes+2) == 's' and
*(yes+3) == '\0'
Similarly, your no variable is a pointer to a three character array, such that Likewise.
*(no+0) == 'n'
*(no+1) == 'o' and
*(no+2) == '\0'
printf("Select: yes or no\n");
fgets(yesno, sizeof(yesno), stdin);
fgets() stops reading after an EOF or a newline. If a newline is read, it is
stored in the buffer. A \0 is stored after the last character in the buffer.
If your hosted environment is consistant with most environments, fgets() will
have captured a buffer that
a) has a newline character following the input data, and
b) has a \0 after the newline character.
Assuming the user entered "no", then yesno will be string of four characters
yesno[0] will be 'n'
yesno[1] will be 'o'
yesno[2] will be '\n' and
yesno[3] will be '\0'
if(strcmp(yes, yesno) == 0)
strcmp() performs a character by character comparison. Even if the user entered
"yes", if the entry was terminated by a newline, then yesno will carry "yes\n",
but yes will point to a string "yes". These are not equal (yesno carries a '\n'
where yes carries a '\0'), and the test fails.
{
printf("yes\n");
}
if(strcmp(no, yesno) == 0)
Similarly, if the user entered "no", and the entry was terminated by a newline,
then yesno will carry "no\n", but no will point to the string "no". Again, the
comparison fails because '\n' is not equal to '\0'.
{
printf("no\n");
}
return 0;
}
One way to fix this would be to change
char *yes = "yes";
char *no = "no";
to
char *yes = "yes\n";
char *no = "no\n";