我的电脑和Arduino的不同尺寸的INT [英] Different int sizes on my computer and Arduino

查看:207
本文介绍了我的电脑和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

这篇关于我的电脑和Arduino的不同尺寸的INT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆