我是否应该禁用C编译器符号/无符号不匹配的警告? [英] Should I disable the C compiler signed/unsigned mismatch warning?
问题描述
微软C编译器警告,当您尝试比较两个变量,一个是签名,另一个是无符号的。例如:
The Microsoft C compiler warns when you try to compare two variables, and one is signed, and the other is unsigned. For example:
int a;
unsigned b;
if ( a < b ) { // warning C4018: '<' : signed/unsigned mismatch
}
有此警告,在世界的历史上,从未患过一个真正的错误?为什么是在那里啊?
Has this warning, in the history of the world, ever caught a real bug? Why's it there, anyway?
推荐答案
哦,是这样。但周围的其他方法。的忽略的这一警告引起了巨大的头痛我一天。我正在写该曲线的图表的函数,并混合符号和无符号的变量。在一个地方,我比较了负数无符号之一:
Oh it has. But the other way around. Ignoring that warning caused a huge headache to me one day. I was writing a function that plotted a graph, and mixed signed and unsigned variables. In one place, i compared a negative number to a unsigned one:
int32_t t; ...
uint32_t ut; ...
if(t < ut) {
...
}
猜猜发生了什么?已签署的数字被提升到无符号的类型,因此是的大于的结尾,即使它是低于0最初。我花了几个小时,直到我发现的bug。
Guess what happened? The signed number got promoted to the unsigned type, and thus was greater at the end, even though it was below 0 originally. It took me a couple of hours until i found the bug.
这篇关于我是否应该禁用C编译器符号/无符号不匹配的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!