fgets(input,sizeof(input),stdin);
if (strcmp(input, "quit") == 0){
exit(-1);
}
If I type quit, it does not exit the program; I’m wondering why this is the case.
By the way input is declared as char *input;.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Trailing newline in your input. See man fgets. Test for “quit” + newline, for example:
I completely missed the last sentence, re
char *input. Depending on the architecture,inputwill be 4 or 8 bytes long. So the code is effectivelywhich doesn’t reflect the real size of memory,
inputpoints to. This might “work” as long as the input is shorter than eight bytes, but will truncate the input, if it is larger. Furthermore, you will get the rest of the input the next time you callfgets.You should either give the real size or take @JonathanLeffler’s advice and declare a char array instead, e.g.
or