comparison of integers of different signs C/C++ Origin: Application & API Last updated 15 Oct 2026

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.

Exact message you may see comparison of integer expressions of different signednesscomparison between signed and unsigned integer expressionscomparison of integers of different signswarning: comparison of integers of different signs
Seen on: C/C++ GCC Clang

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 i against container.size()
  • Comparing a possibly negative int with size_t/unsigned
  • Mixing long and unsigned long variables
  • Arithmetic that subtracts from size() and wraps below zero

⚡ Quick fix

  1. Use size_t or auto for indices
  2. Prefer range-based for loops
  3. Check i >= 0 before casting

Detailed fix by platform

C++

  1. for (std::size_t i = 0; i < v.size(); ++i) or for (const auto& x : v)
  2. C++20: std::ssize(v) returns a signed size

Code examples

The classic loop

cpp
#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

  1. Types — Which operand is unsigned?
  2. Range — Can the signed value be negative?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

C4996 — MSVC: error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead (C/C++)C4996call of overloaded is ambiguous — C++: error: call of overloaded 'f(int)' is ambiguous (C/C++)call of overloaded is…control reaches end of non-void function — C/C++: warning: control reaches end of non-void function [-Wreturn-type] (C/C++)control reaches end o…does not name a type — C++: error: 'string' does not name a type (C/C++)does not name a typeformat expects argument of type — C: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=] (C/C++)format expects argume…implicit declaration of function — C: warning: implicit declaration of function 'foo' [-Wimplicit-function-declaration] (C/C++)implicit declaration …invalid conversion from — C++: error: invalid conversion from 'const char*' to 'char*' [-fpermissive] (C/C++)invalid conversion fr…ISO C++ forbids comparison between pointer and integer — C++: error: ISO C++ forbids comparison between pointer and integer [-fpermissive] (C/C++)ISO C++ forbids compa…compariso…

Explore the map of this error Full error map →

🧠 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.