Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the value of var for the following C statement?

var = strcmp("Hello", "World");

 

  • A.-1
  • B.1
  • C.strcmp has void return-type

Answer: A

Correct answer: -1

strcmp compares character by character, and the first pair decides it: 'H' is 72 while 'W' is 87, so the first string sorts before the second and the result is negative. The standard promises only the sign, not the magnitude - some libraries return the difference, here -15 - so code should test < 0 rather than == -1.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.