Answer the question
In order to leave comments, you need to log in
Why is Arduino int 2 bytes and not 4?
I'm used to the fact that all languages have int 4 bytes and have a dimension of -2 147 483 648 to 2 147 483 647, but after looking at the documentation I saw that arduino has 2 bytes and the range is -32.768 to 32.767. It has something to do with the processor architecture.
Answer the question
In order to leave comments, you need to log in
It's not about the language, but about the C++ standard (on which the Arduino Wiring is based), which assumes the type is int
equal to some size, depending on the target platform :
The reason is the optimal size of the type for the processor.
With a high degree of probability, the sizes of other types also differ there, including pointers ( void*
).
Really got used to it. The C++ standard only regulates the minimum value ranges for types , so the Arduino implementation fits perfectly into the standard.
Want to use guaranteed length types - use int32_t
The computing core of the microcontroller has a capacity of 8 bits. Therefore, all registers that the microcontroller operates on are either 8-bit or 16-bit.
8-bit ALU, for example. add two numbers uses 1 operation for numbers of type uint8_t, or two
operations for numbers of type unit16_t. For numbers of type uint32_t, 4 operations are required.
The same with read-write operations from memory to registers and to memory.
That is, to simply find out if an int variable is equal to zero, 4 operations are required? It would be strange.
The type int is the main type in the C language. In this case, it would be used only in exceptional cases.
So on 8-bit and 16-bit platforms the size of an int is 16 bits, on 32-bit platforms it's 32 bits, on 64-bit platforms it's 64, and so on.
The char type is even more confusing.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question