Various sizes on my computer and Arduino

I am working on a backup time project by creating server code for the Arduino Duemilanove, but before testing this code on the controller, I test it on my own machine (MacBook based on OS X). I use ints in some places, and I am concerned that this will cause strange errors when the code is compiled and run on an Arduino Duemilanove, because Arduino treats ints as 2 bytes, and my macbook treats ints as 4 bytes. I'm not a hardcore C and C ++ programmer, so I'm a little worried about how an experienced programmer can handle this situation. Should I restrict the code with a typedef that includes my own definition and int that is limited to two bytes? Or is there another way?

+3
source share
5 answers

It is best to use a title stdint.h. It defines typedefs that explicitly reference the signature and size of your variables. For example, a 16-bit unsigned integer is equal uint16_t. This is part of the C99 standard, so it is available almost everywhere. Cm:

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

+11
source

C , int , , , , -32768 32767 - , 32- 32- , , , - 16- int. uint16_t uint32_t, arduino ; , .

+6

- , , :

  • -32767 32767 , int;
  • , -2147483647 2147483647 , long;
  • long long.
  • -32767 32767 , short ( signed char, -127 127 ).

, (.. sizeof , ), .

stdint.h , - . .

+4

-32,768 , +32,767? , . , stdint.h , singned und unsigned, intN_t/uintN_t (N = ). C99, . , , , (16 ), , .

+2

int, /.

short long

0

Source: https://habr.com/ru/post/1755622/


All Articles