size_t and int comparison

From: tings (tings668_at_hotmail.com)
Date: 01/09/05


Date: Sun, 09 Jan 2005 20:54:19 GMT

for (int i=0;i < strlen(pathcmd);i++){//this line cause a warning

warning C4018: '<' : signed/unsigned mismatch

strlen returns a number of type 'size_t'. size_t is an unsigned type and
you are comparing it to an int, a signed type.

Two solutions to remove the warning:

1. Change the type of the variable 'i' to 'size_t'.
2. staic_cast i to "unsigned" type.

Which way is better in C++?



Relevant Pages

  • Re: warning - comparing a signed value to an unsinged value
    ... > I was comparing the result of a strlencall to an int that was used as ... One is a signed type and one is an ... > unsigned type. ...
    (comp.lang.c)
  • Re: size_t compare with zero
    ... int main ... comparison i>=0 is true and loop executes ... If you think you have stored a negative value in it, you've misunderstood how conversion from a signed type to an unsigned type is performed. ...
    (comp.lang.c)
  • Re: comparing sizes of two structures
    ... Casting size_t to an unsigned type makes far more sense than a signed ... It's easier to write %d and (int) in ten line programs whose whole ... Besides, ironically, cast to unsigned long is almost as bad ... Once something goes out the int range, ...
    (comp.lang.c)
  • Re: comparing sizes of two structures
    ... Casting size_t to an unsigned type makes far more sense than a signed ... It's easier to write %d and (int) in ten line programs whose whole ... purpose is to print structure member offsets. ...
    (comp.lang.c)
  • Re: define a data type of 1 bit size
    ... > Is there any way I can define a data type that uses only 1 bit. ... even int is fine - just use zero for 0 and non-zero for 1. ... perhaps get better performance by using an unsigned type such as unsigned ... typically more useful in these embedded environments where memory may be ...
    (comp.lang.c)