A warning - comparison between signed and unsigned integer expressions
It is usually a good idea to declare variables as unsigned
or size_t
if they will be compared to sizes, to avoid this issue. Whenever possible, use the exact type you will be comparing against (for example, use std::string::size_type
when comparing with a std::string
's length).
Compilers give warnings about comparing signed and unsigned types because the ranges of signed and unsigned ints are different, and when they are compared to one another, the results can be surprising. If you have to make such a comparison, you should explicitly convert one of the values to a type compatible with the other, perhaps after checking to ensure that the conversion is valid. For example:
unsigned u = GetSomeUnsignedValue();
int i = GetSomeSignedValue();
if (i >= 0)
{
// i is nonnegative, so it is safe to cast to unsigned value
if ((unsigned)i >= u)
iIsGreaterThanOrEqualToU();
else
iIsLessThanU();
}
else
{
iIsNegative();
}
C++ Comparison between signed and unsigned integer expressions
The most straightforward way to fix this is to make i
an unsigned variable instead of a signed one. You can use size_t
to match the return type of strlen:
size_t length = strlen(m_sku);
char *arr = new char[length]();
for (size_t i = 0; i <= length; i++) {
arr[i] = m_sku[i];
}
But be careful since this same replacement doesn't work with loops that count down towards 0.
// oops! This is an infinite loop:
for (size_t i = length-1; i >=0; i--) {
arr[i] = m_sku[i];
}
warning: comparison between signed and unsigned integer expressions..how to solve it?
std::vector::size
returns a value of size_type
, which is Unsigned integral type (usually std::size_t
).
Your loop count variable is of type long
which is a signed type. So in the loop condition you are comparing a signed and an unsigned type.
The solution is simple: Use std::vector<long int>::size_type
(or maybe even size_t
) instead of long
.
C++ Compile Error (comparison between signed and unsigned integer expressions)
Initializing an unsigned integer to -1
is well defined and sets the unsigned integer to its maximal value. So using -1 to represent an error condition is OK. To get rid of the warning, you have few options:
1) Use a static_cast
. This indicates that you are aware of the conversion and it is intentional:
if empty_space == static_cast<DWORD>(-1)) { ...
2) Use std::numeric_limits<DWORD>::max()
instead of -1
. This will require including the limits
header.
if (empty_space == std::numeric_limits<DWORD>::max()) { ...
Comparison between signed and unsigned integer expressions
This line is the problem
for (int i = 0; i < sizeof(message) / sizeof(message[0]); i++)
The operator sizeof
has the return type of std::size_t
which is therefore what you should use for the type of your variable i
. std::size_t
is an unsigned type, so the compiler is warning you that you are comparing a signed type (int
) to an unsigned type, because the comparison is potentially unsafe if the value of one variable is not in the representable range of in the other type.
for (std::size_t i = 0; i < sizeof(message) / sizeof(message[0]); ++i)
Or simply use a range-based for loop.
for (auto i : message)
{
std::cout << i; // i is a char from the message array
}
Related Topics
How to an Share Address Mapping Between Two Unrelated Processes on Linux
System("Command") Produces an Error; But It Works When Invoked Directly from Bash Prompt
How to Compile Curlpp on Ubuntu
How to Enable C++11 in Qt Creator
Why Does C++ Need the Scope Resolution Operator
What Exactly Is the L Prefix in C++
C++ Program Converts Fahrenheit to Celsius
What Does Template <Unsigned Int N> Mean
Get the Compiler Options from a Compiled Executable
Does "Const" Just Mean Read-Only or Something More
How to Convert Char* to Wchar_T*
Is Auto as a Parameter in a Regular Function a Gcc 4.9 Extension
Efficient Unsigned-To-Signed Cast Avoiding Implementation-Defined Behavior
How Would You Implement Your Own Reader/Writer Lock in C++11