1. What does this integer comparison print?
#include <stdio.h>
int main(void) {
int a = 5, b = 5, c = 6;
printf("%d\n", a == b);
printf("%d\n", a == c);
printf("%d\n", a != c);
return 0;
}
Output:
1
0
1
Comparison operators produce int 1 (true) or 0 (false). a == b is 1, a == c is 0, and a != c is 1. There is no boolean type in C — the results are plain integers that can be printed with %d.
2. What does this character comparison print?
#include <stdio.h>
int main(void) {
printf("%d\n", 'a' == 97);
printf("%d\n", 'A' == 65);
printf("%d\n", 'a' < 'b');
printf("%d\n", 'A' < 'a');
return 0;
}
Output:
1
1
1
1
Characters compare by their integer code point. 'a' == 97 and 'A' == 65 hold in ASCII. 'a' < 'b' because 97 < 98. And 'A' < 'a' because uppercase 65 is less than lowercase 97 — the uppercase-to-lowercase ordering is a common interview trap.
3. What does this a < b < c code print?
#include <stdio.h>
int main(void) {
int a = 1, b = 2, c = 3;
printf("%d\n", a < b < c);
printf("%d\n", c < b < a);
return 0;
}
Output:
1
1
a < b < c does not chain like Python — it parses as (a < b) < c. a < b is 1, then 1 < c is 1 < 3 → 1. And c < b < a is (c < b) < a = 0 < a = 0 < 1 → 1. The classic trap: relational operators are left-associative and yield 0/1, so both lines print 1.
4. What does this signed/unsigned comparison print?
#include <stdio.h>
int main(void) {
int i = -1;
unsigned int u = 1;
printf("%d\n", i < u);
printf("%u\n", (unsigned int)i);
return 0;
}
Output:
0
4294967295
In a signed/unsigned comparison, the signed value is converted to unsigned. (unsigned int)(-1) is the largest 32-bit value, 4294967295. So i < u becomes 4294967295 < 1, which is 0. This silent conversion is behind the famous “infinite loop when comparing int i to strlen” bug.
5. What does this equality-after-assignment print?
#include <stdio.h>
int main(void) {
int a = 5;
if (a = 10)
printf("true\n");
else
printf("false\n");
printf("%d\n", a);
return 0;
}
Output:
true
10
if (a = 10) uses the assignment operator =, not ==. It assigns 10 to a and the condition tests the assigned value, which is 10 (truthy) — so "true" prints and a is now 10. Writing if (a = 10) instead of if (a == 10) is one of C’s most famous bugs; many codebases write if (10 == a) to make a typo a compile error.
6. What does this pointer comparison print?
#include <stdio.h>
int main(void) {
int x = 5, y = 5;
int *p = &x, *q = &x;
int *r = &y;
printf("%d\n", p == q);
printf("%d\n", p == r);
printf("%d\n", *p == *r);
return 0;
}
Output:
1
0
1
Comparing pointers with == compares the addresses they hold, not the values they point to. p and q both point to x, so p == q is 1. p and r point to different variables, so p == r is 0. But the pointed-to values are equal: *p == *r is 5 == 5 → 1.
7. What does this NULL comparison print?
#include <stdio.h>
int main(void) {
int *p = NULL;
int *q = 0;
printf("%d\n", p == NULL);
printf("%d\n", p == q);
printf("%d\n", NULL == 0);
return 0;
}
Output:
1
1
1
NULL is defined as a null pointer constant — effectively 0 (possibly cast to void*). A null pointer compares equal to NULL, to 0, and to another null pointer. So all three comparisons print 1. (Comparing a null pointer with a plain 0 is allowed as long as the 0 is treated as a null pointer constant.)
8. What does this float-equality print?
#include <stdio.h>
int main(void) {
float a = 0.1f;
float b = 0.1f;
printf("%d\n", a == b);
printf("%d\n", 0.1f == 0.1);
printf("%d\n", 0.1 + 0.2 == 0.3);
return 0;
}
Output:
1
0
0
a == b compares two identical float values — 1. But 0.1f == 0.1 compares a float (promoted to double representation of the float) against a full-precision double, and the two round differently — 0. And 0.1 + 0.2 == 0.3 is 0 because 0.1 + 0.2 is 0.30000000000000004 in binary floating point. Comparing floats for exact equality is the #1 float interview trap.
9. What does this short-circuit comparison print?
#include <stdio.h>
int main(void) {
int x = 0;
printf("%d\n", 0 && (x = 1));
printf("%d\n", 1 || (x = 2));
printf("%d\n", x);
return 0;
}
Output:
0
1
0
0 && (x = 1) short-circuits — the right side never runs, so x is untouched and the result is 0. 1 || (x = 2) short-circuits too — x = 2 never runs. So x remains 0. The right operand of &&/|| is evaluated only when it can affect the result.
10. What does this sizeof comparison print?
#include <stdio.h>
int main(void) {
int a = 0;
printf("%d\n", sizeof(int) > sizeof(char));
printf("%d\n", sizeof(a) == sizeof(int));
printf("%d\n", sizeof(char) == 1);
return 0;
}
Output:
1
1
1
sizeof(int) > sizeof(char) is 4 > 1 → 1. sizeof(a) is the size of a’s type, int → 1. And sizeof(char) == 1 is guaranteed by the standard → 1. All three print 1.
11. What does this string comparison print?
#include <stdio.h>
#include <string.h>
int main(void) {
char s1[] = "abc";
char s2[] = "abc";
printf("%d\n", s1 == s2);
printf("%d\n", strcmp(s1, s2) == 0);
return 0;
}
Output:
0
1
s1 == s2 compares the array addresses, which are different local arrays — 0. To compare string contents you must use strcmp(s1, s2), which returns 0 when they are equal, so strcmp(s1, s2) == 0 is 1. Comparing string contents with == is the most common C string bug.
12. What does this strcmp ordering print?
#include <stdio.h>
#include <string.h>
int main(void) {
printf("%d\n", strcmp("apple", "banana") < 0);
printf("%d\n", strcmp("abc", "abd") < 0);
printf("%d\n", strcmp("abc", "abcd") < 0);
printf("%d\n", strcmp("a", "A") > 0);
return 0;
}
Output:
1
1
1
1
strcmp compares strings byte-by-byte and returns a negative/zero/positive value. "apple" < "banana", "abc" < "abd", and "abc" < "abcd" (a shorter prefix is “less” when one runs out). "a" > "A" because 'a' (97) is greater than 'A' (65). All four comparisons print 1.
13. What does this loop-condition comparison print?
#include <stdio.h>
int main(void) {
int i;
for (i = 0; i < 5; i++)
;
printf("%d\n", i);
for (i = 5; i > 0; i--)
;
printf("%d\n", i);
return 0;
}
Output:
5
0
After for (i = 0; i < 5; i++), the loop exits when the condition i < 5 is false — at i == 5. After for (i = 5; i > 0; i--), it exits when i > 0 is false — at i == 0. The final value of the loop counter is the first one that fails the condition.
14. What does this comparison-with-not print?
#include <stdio.h>
int main(void) {
int a = 0, b = 1;
printf("%d\n", !a == 1);
printf("%d\n", !b == 0);
printf("%d\n", !a == !b);
return 0;
}
Output:
1
1
0
! has higher precedence than ==, so !a == 1 is (!a) == 1 = 1 == 1 → 1. Similarly !b == 0 is 0 == 0 → 1. And !a == !b is 1 == 0 → 0. Precedence makes these parse as (!a) == 1, not !(a == 1).
15. What does this strlen signed/unsigned comparison print?
#include <stdio.h>
#include <string.h>
int main(void) {
const char *s = "hello";
if (strlen(s) > -1)
printf("greater\n");
else
printf("not greater\n");
return 0;
}
Output:
not greater
strlen returns size_t, which is unsigned. In strlen(s) > -1, the -1 is converted to size_t — the largest representable value. So 5 > 4294967295... is 0 (false) and the else branch prints "not greater". This is the famous C pitfall: (size_t)x > -1 is always false.
Premium Content
Unlock Comparison Questions - Part 1 and all premium lessons with a subscription.
From ₹199.99/year — See plans