'comparison is always true due to limited range of data type' warning in C?

后端 未结 6 1336
一向
一向 2021-01-12 10:22

I have the following code

//Point.h
#define WIDTH 8
#define HEIGHT 8

typedef struct Point
{
  char x;
  char y;
} Point;

//Board.c
#include 

        
6条回答
  •  太阳男子
    2021-01-12 10:37

    The char type may be signed or unsigned. It depends on your compiler vendor's choice. There might even be a compiler option available. Evidently, char is unsigned for you, so it's always greater than or equal to zero, and thus the compiler warns you.

    You're using char here to represent "a numeric type that takes up minimal memory." In that case, I recommend explicitly using signed char or unsigned char. (Each is distinct from plain char, despite char having to be either signed or unsigned.) Reserve char for when you're holding character data. For numeric data, use one of the other two types.

提交回复
热议问题