M
Martin
Below is some sample code to illustrate two warnings I get using a Hitachi
compiler. When I use the GCC compiler I get a clean compile. I'd be grateful
if someone could explain what the warnings actually mean as they are rather
crytic, to me anyway.
When I change the type of LOOKUP_MAX from size_t to int, I get a clean
compile in the Hitachi compiler.
#include <stdio.h>
#define NELEMENTS(a) (sizeof(a) / sizeof(a[0]))
/* macro for portable modulo operations */
#if (-1 % 2 > 0)
#define IMOD(i, j) ((i) % (j))
#else
#define IMOD(i, j) (((i) % (j)) < 0 ? ((i) % (j)) + j : ((i) % (j)))
#endif
char *lookup[] =
{
"ABC",
"DEF",
"GHI",
"JKL"
};
const size_t LOOKUP_MAX = NELEMENTS(lookup);
#include <stdio.h>
int main( void )
{
int val;
/* code where user decides whether to increment or decrement value
removed */
/* ... */
/* User wants to increment value, keep in range [0, LOOKUP_MAX] */
val = IMOD(val+1, LOOKUP_MAX);
/* The above line of code generates two warnings:
* Unsigned compare always true/false
* Conditional expression always true/false.
*/
return 0; /* success */
}
Martin
http://martinobrien.co.uk/
compiler. When I use the GCC compiler I get a clean compile. I'd be grateful
if someone could explain what the warnings actually mean as they are rather
crytic, to me anyway.
When I change the type of LOOKUP_MAX from size_t to int, I get a clean
compile in the Hitachi compiler.
#include <stdio.h>
#define NELEMENTS(a) (sizeof(a) / sizeof(a[0]))
/* macro for portable modulo operations */
#if (-1 % 2 > 0)
#define IMOD(i, j) ((i) % (j))
#else
#define IMOD(i, j) (((i) % (j)) < 0 ? ((i) % (j)) + j : ((i) % (j)))
#endif
char *lookup[] =
{
"ABC",
"DEF",
"GHI",
"JKL"
};
const size_t LOOKUP_MAX = NELEMENTS(lookup);
#include <stdio.h>
int main( void )
{
int val;
/* code where user decides whether to increment or decrement value
removed */
/* ... */
/* User wants to increment value, keep in range [0, LOOKUP_MAX] */
val = IMOD(val+1, LOOKUP_MAX);
/* The above line of code generates two warnings:
* Unsigned compare always true/false
* Conditional expression always true/false.
*/
return 0; /* success */
}
Martin
http://martinobrien.co.uk/