C/C++: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' [-Wsign-compare]
A signed integer is compared with an unsigned one; the signed value is converted to unsigned, so a negative number becomes huge.
comparison of integer expressions of different signednesscomparison between signed and unsigned integer expressionscomparison of integers of different signswarning: comparison of integers of different signs Meaning
for (int i = 0; i < v.size(); ++i) triggers it because size() returns an unsigned size_t. With -1, the comparison -1 < 5u is false. Use size_t for indices, range-for, or cast deliberately after checking the sign.
Common causes
- Looping with
int iagainstcontainer.size() - Comparing a possibly negative
intwithsize_t/unsigned - Mixing
longandunsigned longvariables - Arithmetic that subtracts from
size()and wraps below zero
⚡ Quick fix
- Use
size_torautofor indices - Prefer range-based for loops
- Check
i >= 0before casting
Detailed fix by platform
C++
for (std::size_t i = 0; i < v.size(); ++i)orfor (const auto& x : v)- C++20:
std::ssize(v)returns a signed size
Code examples
The classic loop
#include <vector>
#include <cstddef>
void run(const std::vector<int>& v) {
// for (int i = 0; i < v.size(); ++i) // warning: comparison of integer expressions of different signedness
for (std::size_t i = 0; i < v.size(); ++i) {}
for (const auto& x : v) { (void)x; } // best: no index at all
}How to diagnose
- Types — Which operand is unsigned?
- Range — Can the signed value be negative?
- Loop — Can a range-for replace the index?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- C4996 MSVC: error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead
- call of overloaded is ambiguous C++: error: call of overloaded 'f(int)' is ambiguous
- control reaches end of non-void function C/C++: warning: control reaches end of non-void function [-Wreturn-type]
Most viewed in C/C++
- AddressSanitizer: heap-use-after-free C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free
- AddressSanitizer: heap-buffer-overflow C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N
- Bus error C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 15 Oct 2026