我的电脑和 Arduino 上的不同整数大小 [英] Different int sizes on my computer and Arduino

查看:20
本文介绍了我的电脑和 Arduino 上的不同整数大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个业余项目,为 Arduino Duemilanove 制作一些服务器代码,但在我在控制器上测试此代码之前,我正在我自己的机器上测试它(基于 OS X 的 macbook).我在某些地方使用了整数,我担心在 Arduino Duemilanove 上编译和运行代码时会出现奇怪的错误,因为 Arduino 将整数处理为 2 个字节,而我的 macbook 将整数处理为 4 个字节.我不是一个铁杆 C 和 C++ 程序员,所以我有点担心一个有经验的程序员会如何处理这种情况.我是否应该使用一个 typedef 来限制代码,该 typedef 包装了我自己的 2 个字节的 int 定义?或者有其他办法吗?

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 标头.它定义了明确引用变量的符号和大小的 typedef.例如,一个 16 位无符号整数是一个 uint16_t.它是 C99 标准的一部分,因此几乎可以在任何地方使用.见:

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 上的不同整数大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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