ProgrammingVery hardProduct company OA · GATE CS
With char *a = "hi", *b = "hi"; what can be said about the value of a == b?
- A. It is always 1, because the contents are equal
- B. It is unspecified: the compiler may store identical string literals once or separately
- C. The comparison does not compile
- 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
- What is printed? c int c = 0; for (int i = 1; i <= 4; i++) for (int j = i; j <= 4; j += i) c++; printf("%d", c);
- Assume 16-bit unsigned short and 32-bit int. What is printed? c unsigned short v = 0x1234; unsigned short w = (unsigned short)(v << 8 | v…
- What is printed? c void conv(int n, int b) { if (n >= b) conv(n / b, b); putchar("0123456789ABCDEF"[n % b]); } int main(void) { conv(255…
- A single C source file contains these three lines at file scope. What happens? c int x; int x; int x = 5;
- Assume 4-byte int, 8-byte double and natural alignment. What is printed? c struct T { double d; int i; }; struct T arr[3]; printf("%td"…
- What is printed? c int len(const char s) { return s ? 1 + len(s + 1) : 0; } int cnt(const char s, char c) { return !s ? 0 : (s == c) +…