Different int sizes on my computer and Arduino

后端 未结 5 501
梦谈多话
梦谈多话 2021-01-21 07:12

Im working on a sparetime project, making some server code to an Arduino Duemilanove, but before I test this code on the controller I am testing it on my own machine (An OS X ba

5条回答
  •  逝去的感伤
    2021-01-21 07:55

    The correct way to handle the situation is to choose the type based on the values it will need to represent:

    • If it's a general small integer, and the range -32767 to 32767 is OK, use int;
    • Otherwise, if the range -2147483647 to 2147483647 is OK, use long;
    • Otherwise, use long long.
    • If the range -32767 to 32767 is OK and space efficiency is important, use short (or signed char, if the range -127 to 127 is OK).

    As long as you have made no other assumptions that these (ie. always using sizeof instead of assuming the width of the type), then your code will be portable.

    In general, you should only need to use the fixed-width types from stdint.h for values that are being exchanged through a binary interface with another system - ie. being read from or written to the network or a file.

提交回复
热议问题