C语言 比较 int 和 size_t
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3642010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
comparing int with size_t
提问by fuddin
If i have a int and a size_t variable,can i compare them like:
如果我有一个 int 和一个 size_t 变量,我可以像这样比较它们:
int i=1;
size_t y=2;
if(i==y)
// do something..
or i have to type-cast one of them?
或者我必须对其中之一进行类型转换?
回答by Philip Potter
It's safe provided the intis zero or positive. If it's negative, and size_tis of equal or higher rank than int, then the intwill be converted to size_tand so its negative value will instead become a positive value. This new positive value is then compared to the size_tvalue, which may (in a staggeringly unlikely coincidence) give a false positive. To be truly safe (and perhaps overcautious) check that the intis nonnegative first:
如果int为零或正数,则是安全的。如果它是负数,并且size_t等于或高于int,那么int将被转换为size_t,因此它的负值将变成正值。然后将这个新的正值与该size_t值进行比较,这可能(以极不可能的巧合)给出误报。为了真正安全(也许是过于谨慎),int首先检查是非负的:
/* given int i; size_t s; */
if (i>=0 && i == s)
and to suppress compiler warnings:
并抑制编译器警告:
if (i>=0 && (size_t)i == s)
回答by Paul Tomblin
size_tis going to be some sort of integer type (although possibly unsigned, so it might generate a warning) so the appropriate casting should be done for you automatically.
size_t将是某种整数类型(尽管可能是无符号的,因此它可能会生成警告),因此应该自动为您完成适当的转换。
As others have already said, you may want to revisit whatever calculation is producing the intand see if you can do it in size_tin the first place if you're computing a required size for something.
正如其他人已经说过的那样,您可能想重新审视正在产生的任何计算,int并首先看看是否可以size_t在计算某物所需的大小时进行计算。
回答by codaddict
It is okay to compare a size_tvalue with an intvalue, the intvalue will be implicitly converted to unsignedtype.
可以将一个size_t值与一个int值进行比较,该int值将被隐式转换为unsigned类型。
Some compilers will issue a warning when you mix signedand unsignedtypes in comparisons. In that case you can explicitly convert the signedvalue to an appropriate unsignedtype to suppress the warning.
当您在比较中混合signed和unsigned键入时,某些编译器会发出警告。在这种情况下,您可以将signed值显式转换为适当的unsigned类型以抑制警告。
回答by Helliarc
If you're going to compare an int type to size_t(or any other library type), you are doing a risky comparison because int is signed and size_t is unsigned, so one of them will be implicitly converted depending on your compiler/platform. The best thing to do, is to rewrite your int i as:
如果您要将 int 类型与 size_t(或任何其他库类型)进行比较,那么您将进行有风险的比较,因为 int 是有符号的,而 size_t 是无符号的,因此其中之一将根据您的编译器/平台进行隐式转换。最好的办法是将你的 int i 重写为:
decltype(y.size()) i = 1;
decltype(y.size()) i = 1;
This assigns your i as the safe type you're trying to compare, and I think it's good practice. This solution is useful in all types of iterators as well. You generally don't want to trust the compiler to cast for you, you can, but it's risky, and an unnecessary risk.
这将您的 i 指定为您要比较的安全类型,我认为这是一种很好的做法。此解决方案也适用于所有类型的迭代器。您通常不想信任编译器为您进行强制转换,您可以,但这是有风险的,而且是不必要的风险。

![C语言 在 C 中将 ascii char[] 转换为十六进制 char[]](/res/img/loading.gif)