为什么为int而不是unsigned int类型多用于C和C ++的for循环?
这是一个相当愚蠢的问题,但为什么是 INT
for循环定义时常用的,而不是 unsigned int类型
在C或C数组++?
This is a rather silly question but why is int
commonly used instead of unsigned int
when defining a for loop for an array in C or C++?
for(int i;i<arraySize;i++){}
for(unsigned int i;i<arraySize;i++){}
我认识使用的好处做其他比数组索引和一些使用C ++的容器时,迭代器的好处时, INT
。难道仅仅是因为通过数组循环时不要紧?或者我应该避免这一切在一起,并使用不同的类型,如为size_t
?
I recognize the benefits of using int
when doing something other than array indexing and the benefits of an iterator when using C++ containers. Is it just because it does not matter when looping through an array? Or should I avoid it all together and use a different type such as size_t
?
这是一个较为普遍的现象,人们往往不使用正确类型的整数。现代C有语义的typedef是在原始的整数类型多preferable。例如一切,这是一个大小应该只输入为为size_t
。如果你使用的语义类型系统为您的应用程序变量,循环变量来使用这些类型的容易得多了。
This is a more general phenomenon, often people don't use the correct types for their integers. Modern C has semantic typedefs that are much preferable over the primitive integer types. E.g everything that is a "size" should just be typed as size_t
. If you use the semantic types systematically for your application variables, loop variables come much easier with these types, too.
和我所看到的一些bug,其中难以察觉,从使用 INT
左右就来了。 code,所有突然坠毁,机上大型矩阵之类的东西。只需用正确的类型正确编码避免了。
And I have seen several bugs that where difficult to detect that came from using int
or so. Code that all of a sudden crashed on large matrixes and stuff like that. Just coding correctly with correct types avoids that.