1. Home
  2. Computer Science MCQs
  3. C Programming
  4. What does strcmp("apple", "apricot") return in C?

Added 30 Nov -0001

What does strcmp("apple", "apricot") return in C?

  1. The number of differing characters in the strings
  2. A negative value, because "apple" sorts first Correct answer
  3. Zero, because the strings are different
  4. A positive value, because "apple" is longer

Correct answer

B. A negative value, because "apple" sorts first

15 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs