ProgrammingVery hardProduct company OA · GATE CS

With char *a = "hi", *b = "hi"; what can be said about the value of a == b?

  1. A. It is always 1, because the contents are equal
  2. B. It is unspecified: the compiler may store identical string literals once or separately
  3. C. The comparison does not compile
  4. D. It is always 0, because each literal is a separate array

Correct answer: B. It is unspecified: the compiler may store identical string literals once or separately

== on pointers compares addresses, not contents. The standard leaves it unspecified whether identical literals share storage, so the result may be 1 or 0 depending on the compiler. Use strcmp to compare contents.

More C Programming questions

Exams that ask this topic