我的电脑和Arduino的不同尺寸的INT

问题描述:

林工作的一个业余项目,使得一些服务器code到一个Arduino Duemilanove,但在此之前我测试我是我自己的机器(一个OS X基础的MacBook)上测试它在控制器上这code。我使用int一些地方,我很担心,当code编译并在Arduino的Duemilanove运行,因为Arduino的处理整数为2个字节,和我的MacBook处理整数为4个字节,这将弹出莫名其妙的错误。我不是一个铁杆的C和C ++程序员,所以我在有点担心有经验的程序员将如何处理这种情况的。
我是否应该限制code与包装我自己的定义和诠释了限制为2个字节的typedef?还是有另一种办法解决?

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 based macbook). I am using ints some places, and I am worried that this will bring up strange errors when code is compiled and run on the Arduino Duemilanove because the Arduino handles ints as 2 bytes, and my macbook handles ints as 4 bytes. Im not a hardcore C and C++ programmer, so I am in a bit of worry how an experienced programmer would handle this situation. Should I restrict the code with a typedef that wrap my own definition of and int that is restricted to 2 bytes? Or is there another way around?

您最好的选择是使用 stdint.h 头。它定义类型定义,明确参考您的变量的符号性和大小。例如,一个16位无符号整数是 uint16_t 。这是C99标准的一部分,所以它更随处可得pretty。参见:

Your best bet is to use the stdint.h header. It defines typedefs that explicitly refer to the signedness and size of your variables. For example, a 16-bit unsigned integer is a uint16_t. It's part of the C99 standard, so it's available pretty much everywhere. See:

http://en.wikipedia.org/wiki/Stdint.h