ProgrammingHardHCL · Tech Mahindra pattern
What is printed?
What is printed?
int x = 5;
if (x = 10)
printf("true %d", x);
else
printf("false %d", x);- A. true 5
- B. false 10
- C. true 10
- D. Compile-time error: = should be == in a condition
Correct answer: C. true 10
x = 10 is an ASSIGNMENT (single =), not a comparison; it assigns 10 to x and the whole assignment expression evaluates to the assigned value, 10, which is treated as true in the if condition (this compiles fine in C, often with just a warning). So the if branch runs, with x now equal to 10, printing true 10 — a classic assignment-vs-comparison trap.
More C Programming questions
- What is printed? c int a = 1, b = 0, c = 1; int result = a || b && c; printf("%d", result);
- What is printed? c int x = 5; int y = x++ + (x = 10); printf("%d", y);
- Assume a 64-bit LP64 ABI. What is printed? c int a = 5, b = 10, c = 15; int arr[3]; arr[0] = &c; arr[1] = &a; arr[2] = &b; int sum = 0…
- Assume a 64-bit LP64 ABI. What is printed? c int a = 100; int p = &a; int q = p; p = NULL; printf("%d %d", (p == NULL), (q));
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; void movePoint(struct Point p, int dx, int dy) { p->x += dx; p->y…
- Assume a 64-bit LP64 ABI. What happens? c int createArray(int n) { int arr[n]; for (int i = 0; i < n; i++) arr[i] = i i; return arr; } int…