ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
int main(void) {
int a[3] = {1, 2, 3};
int b[3] = {1, 2, 3};
printf("%d", a == b);
}- A. 3
- B. 0
- C. 1
- D. Compilation error: cannot compare arrays with ==
Correct answer: B. 0
a and b decay to pointers to their own separate, distinct memory regions; `a == b` compares these two different addresses, which are never equal even though the arrays hold identical contents: 0 (false).
More C Programming questions
- What is printed? c int main(void) { char a[] = "test"; char b[] = "test"; printf("%d %d", a == b, strcmp(a, b) == 0); }
- What is printed? c int main(void) { int x = 100; long y = x; long long z = y 1000000000L; printf("%lld", z); }
- What is printed? c int main(void) { int n = 5; int result = 1; int i = 1; while (i <= n) { result = i; i++; } printf("%d", result); }
- Assume 32-bit int. What is printed? c int a = 4, b = 7; int max = (a > b) ? a : b; int min = (a < b) ? a : b; printf("%d %d %d", max, min…
- What happens when this runs? c int p; if (p != NULL) { printf("%d", p); } else { printf("null"); }
- Assume a 64-bit LP64 ABI. What is printed? c struct Inventory { char name[16]; int quantity; double price; }; void restock(struct…