Why is int rather than unsigned int used for C and C++ for loops?

后端 未结 10 2075
日久生厌
日久生厌 2020-12-02 15:50

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++?

<         


        
10条回答
  •  旧巷少年郎
    2020-12-02 16:10

    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.

    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.

提交回复
热议问题