您的位置:首页 > 其它

KeilMDK编译警告:warning: #514-D: pointless comparison of unsigned integer with a negative constant

2015-06-02 12:02 731 查看
编译包含下面一段代码的程序,代码如下:

if (cmd == -1)
{
    plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 401 Unauthorized\r\nContent-Type: text/html\r\n\r\n<h1>401 Unauthorized</h1>"));
    goto SENDTCP;
}
编译结果针对这行,做出如下警告:

web_server.c(288): warning: #514-D: pointless comparison of unsigned integer with a negative constant

大意为:一个无意义的比较在无符号整型和一个负数常量

查找原因发现:发现 cmd 变量定义形式为:

unsigned char cmd;

解决办法如下:

if (cmd == (unsigned char)-1)
{
    plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 401 Unauthorized\r\nContent-Type: text/html\r\n\r\n<h1>401 Unauthorized</h1>"));
    goto SENDTCP;
}
可以思考的问题:

把 不同类型的-1 强制类型转换后的结果为多少?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: