1. What does this std::string vs char* comparison print?
#include <iostream>
#include <string>
int main() {
std::string a = "hi";
std::string b = "hi";
const char *c = "hi";
const char *d = "hi";
std::cout << (a == b) << "\n";
std::cout << (a == c) << "\n";
std::cout << (c == d) << "\n";
return 0;
}
Output:
1
1
1
std::string == compares content in both cases — a == b and a == c (there’s an overload for comparing string with const char*). But c == d compares pointer addresses, and whether identical literals share an address is implementation-defined — here the compiler merges them, so it’s 1. Never rely on char* == for text comparison.
2. What does this == on distinct strings print?
#include <iostream>
#include <string>
int main() {
std::string a = "abc";
std::string b = "abd";
std::cout << (a == b) << "\n";
std::cout << (a < b) << "\n";
std::cout << (a.compare(b) < 0) << "\n";
return 0;
}
Output:
0
1
1
"abc" != "abd", so a == b is 0. a < b compares lexicographically: 'c' < 'd', so it’s 1. a.compare(b) returns a negative value, so a.compare(b) < 0 is also 1. The < operator and compare() agree — both use lexicographic ordering.
3. What does this == on equal numbers print?
#include <iostream>
int main() {
int a = 100;
int b = 100;
int c = 200;
std::cout << (a == b) << "\n";
std::cout << (a == c) << "\n";
std::cout << (a <= c) << "\n";
return 0;
}
Output:
1
0
1
100 == 100 is 1, 100 == 200 is 0, 100 <= 200 is 1. Comparison of built-in numeric types is value-based and always well-defined — no interning or reference semantics like in some other languages.
4. What does this float comparison print?
#include <iostream>
int main() {
float a = 0.1f;
double b = 0.1;
float c = 0.1f;
std::cout << (a == c) << "\n";
std::cout << (a == b) << "\n";
std::cout << (0.1f == 0.1) << "\n";
return 0;
}
Output:
1
0
0
a == c compares two identical floats → 1. a == b promotes a to double and compares against the full-precision 0.1; the float’s rounded representation differs from the double’s, so → 0. Same for 0.1f == 0.1. Comparing floats across precisions (or at all) is the #1 C++ float interview trap.
5. What does this reference comparison print?
#include <iostream>
int main() {
int x = 10;
int &r = x;
int *p = &x;
std::cout << (r == x) << "\n";
std::cout << (p == &x) << "\n";
std::cout << (*p == r) << "\n";
return 0;
}
Output:
1
1
1
r is a reference — it is x, so r == x is 1. p == &x compares addresses, which match → 1. *p == r compares the value (10 == 10) → 1. References compare by value; pointers by address.
6. What does this char comparison print?
#include <iostream>
int main() {
char c = 'A';
std::cout << (c == 65) << "\n";
std::cout << ('a' < 'b') << "\n";
std::cout << ('A' < 'a') << "\n";
std::cout << ('0' == 48) << "\n";
return 0;
}
Output:
1
1
1
1
char compares by its integer code point. 'A' == 65 and '0' == 48 hold in ASCII. 'a' < 'b' since 97 < 98. 'A' < 'a' since uppercase 65 < 97 lowercase. Character comparison is integer comparison.
7. What does this == on bool print?
#include <iostream>
int main() {
bool t = true;
bool f = false;
std::cout << (t == true) << "\n";
std::cout << (f == false) << "\n";
std::cout << (t == 1) << "\n";
std::cout << (f == 0) << "\n";
return 0;
}
Output:
1
1
1
1
bool values compare equal to true/false, and true promotes to 1 and false to 0 in comparisons. So all four comparisons print 1. bool is not a numeric type, but it converts to int for arithmetic/comparison.
8. What does this integer-promotion comparison print?
#include <iostream>
int main() {
char c = 'A';
unsigned int u = 1;
int i = -1;
std::cout << (static_cast<int>(c) < 200) << "\n";
std::cout << (i < u) << "\n";
return 0;
}
Output:
1
0
c ('A' = 65) promotes to int and 65 < 200 → 1. In i < u, the int is converted to unsigned, so -1 becomes 4294967295, and 4294967295 < 1 is 0. Signed/unsigned mixing silently produces a huge value — the source of many C++ bugs.
9. What does this == on pointers-to-array print?
#include <iostream>
int main() {
int arr[3] = {1, 2, 3};
std::cout << (arr == &arr[0]) << "\n";
std::cout << (arr == &arr[1]) << "\n";
std::cout << (arr + 1 == &arr[1]) << "\n";
return 0;
}
Output:
1
0
1
arr decays to &arr[0], so arr == &arr[0] is 1. &arr[1] points to element 1, so arr == &arr[1] is 0. arr + 1 points to element 1, matching &arr[1] → 1. Note &arr (pointer to the whole array, type int(*)[3]) is a distinct type — comparing it to a plain int* doesn’t even compile without a cast.
10. What does this == after assignment print?
#include <iostream>
int main() {
int a = 5;
if (a == 5)
std::cout << "eq ";
if (a = 5)
std::cout << "assigned ";
std::cout << a << "\n";
return 0;
}
Output:
eq assigned 5
a == 5 is true → prints "eq". a = 5 is an assignment; the condition tests the assigned value 5 (truthy), so "assigned" prints too. a is 5. The = vs == mixup is the most common C-family bug.
11. What does this strcmp-equivalent comparison print?
#include <iostream>
#include <cstring>
int main() {
char a[] = "hello";
char b[] = "hello";
std::cout << (strcmp(a, b) == 0) << "\n";
std::cout << (a == b) << "\n";
return 0;
}
Output:
1
0
strcmp(a, b) == 0 compares the string contents → 1. a == b compares the addresses of two distinct arrays → 0. In C++ you should use std::string instead, but this shows why raw C-string comparison with == is wrong.
12. What does this == on two array elements print?
#include <iostream>
int main() {
int arr[] = {10, 20, 10};
std::cout << (arr[0] == arr[1]) << "\n";
std::cout << (arr[0] == arr[2]) << "\n";
std::cout << (arr[1] > arr[2]) << "\n";
return 0;
}
Output:
0
1
1
arr[0] is 10, arr[1] is 20, arr[2] is 10. So 10 == 20 → 0, 10 == 10 → 1, 20 > 10 → 1. Element comparison is plain value comparison.
13. What does this iterator comparison print?
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4};
std::cout << (v.begin() < v.end()) << "\n";
std::cout << (v.end() - v.begin()) << "\n";
std::cout << (*v.begin() < v[3]) << "\n";
return 0;
}
Output:
1
4
1
v.begin() < v.end() compares iterator positions → 1. v.end() - v.begin() is the element count, 4. *v.begin() is 1, v[3] is 4, so 1 < 4 → 1. Iterator relational comparisons work for random-access iterators like vector’s.
14. What does this == on returned values print?
#include <iostream>
int square(int x) { return x * x; }
int main() {
std::cout << (square(3) == square(-3)) << "\n";
std::cout << (square(3) == 9) << "\n";
std::cout << (square(4) > 15) << "\n";
return 0;
}
Output:
1
1
1
square(3) and square(-3) both return 9, so 9 == 9 → 1. square(3) == 9 → 1. square(4) is 16 > 15 → 1. Function results are ordinary values for comparison.
15. What does this == on member access print?
#include <iostream>
struct Point {
int x, y;
};
int main() {
Point p1 = {1, 2};
Point p2 = {1, 2};
std::cout << (p1.x == p2.x) << "\n";
std::cout << (p1.y == p2.y) << "\n";
std::cout << (p1.x + p1.y == p2.x + p2.y) << "\n";
return 0;
}
Output:
1
1
1
There’s no default == for structs, so you compare member by member. p1.x == p2.x is 1, p1.y == p2.y is 1, and the sums 3 == 3 → 1. To get p1 == p2 you’d need to define operator== — which is what C++20’s == default does.
Premium Content
Unlock Comparison Questions - Part 1 and all premium lessons with a subscription.
From ₹199.99/year — See plans