ProgrammingHardHCL · Tech Mahindra pattern
What is printed?
What is printed?
int main(void) {
char a[] = "test";
char b[] = "test";
printf("%d %d", a == b, strcmp(a, b) == 0);
}- A. 1 1
- B. 0 0
- C. 1 0
- D. 0 1
Correct answer: D. 0 1
a and b are two SEPARATE char arrays (each its own copy of the string), so a==b compares distinct addresses: false (0). strcmp compares their CONTENTS character by character, which are identical: returns 0, so `== 0` is true (1).
More C Programming questions
- 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…
- Assume 32-bit int. What is printed? c #define SWAP(a, b, type) do { type t = a; a = b; b = t; } while (0) int main(void) { int x = 1, y =…