This content relates to a deprecated version of Mbed
Mbed 2 is now deprecated. For the latest version please see the Mbed OS documentation.
C/C++ provides various data types that can be used in your programs.
In general, you'd commonly use:
- int for most variables and "countable" things (for loop counts, variables, events)
- char for characters and strings
- float for general measurable things (seconds, distance, temperature)
- uint32_t for bit manipulations, especially on 32-bit registers
- The appropriate stdint.h types for storing and working with data explicitly at the bit level
Integer Data Types¶
|C type||stdint.h type||Bits||Sign||Range|
|char||uint8_t||8||Unsigned||0 .. 255|
|signed char||int8_t||8||Signed||-128 .. 127|
|unsigned short||uint16_t||16||Unsigned||0 .. 65,535|
|short||int16_t||16||Signed||-32,768 .. 32,767|
|unsigned int||uint32_t||32||Unsigned||0 .. 4,294,967,295|
|int||int32_t||32||Signed||-2,147,483,648 .. 2,147,483,647|
|unsigned long long||uint64_t||64||Unsigned||0 .. 18,446,744,073,709,551,615|
|long long||int64_t||64||Signed||-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807|
Floating Point Data Types¶
|C type||IEE754 Name||Bits||Range|
|float||Single Precision||32||-3.4E38 .. 3.4E38|
|double||Double Precision||64||-1.7E308 .. 1.7E308|
The ARMv7-M architecture used in mbed microcontrollers is a 32-bit architecture, so standard C pointers are 32-bits.
Whilst most types are signed by default (short, int, long long), char is unsigned by default.
Because the natural data-size for an ARM processor is 32-bits, it is much more preferable to use int as a variable than short; the processor may actually have to use more instructions to do a calculation on a short than an int!
In code ported from other platforms, especially 8-bit or 16-bit platforms, the data types may have had different sizes. For example, int may have been represented as 16-bits. If this size has been relied on, some of the code may need updating to make it more portable. In addition, it is quite common that programmers will have defined their own types (UINT8, s8, BYTE, WORD, ..); it is probably beter to convert this to the stdint.h types, which will be naturally portable across platforms.