About the use of signed integers in C family of languages

后端 未结 5 1010
一整个雨季
一整个雨季 2021-01-11 17:57

When using integer values in my own code, I always try to consider the signedness, asking myself if the integer should be signed or unsigned.

When I\'m sure the valu

5条回答
  •  有刺的猬
    2021-01-11 18:27

    • a signed return value might yield more information (think error-numbers, 0 is sometimes a valid answer, -1 indicates error, see man read) ... which might be relevant especially for developers of libraries.

    • if you are worrying about the one extra bit you gain when using unsigned instead of signed then you are probably using the wrong type anyway. (also kind of "premature optimization" argument)

    • languages like python, ruby, jscript etc are doing just fine without signed vs unsigned. that might be an indicator ...

提交回复
热议问题