What does strcmp("apple", "apricot") return in C?
Correct answer
B. A negative value, because "apple" sorts first
15 views
B. A negative value, because "apple" sorts first
15 views
strcmp compares characters by their unsigned char codes and stops at the first mismatch, where 'p' equals 'p' but the third character 'p' is less than 'r', so a negative value is returned. Only equality gives zero, and the exact negative or positive magnitude is implementation-defined, so tests should check sign rather than a specific number. strncmp does the same comparison but stops after n characters.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.