C Data Types

Mbed OS 2 and Mbed OS 5

This is the handbook for Mbed OS 2. If you’re working with Mbed OS 5, please see the new handbook.

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 typestdint.h typeBitsSignRange
charuint8_t8Unsigned0 .. 255
signed charint8_t8Signed-128 .. 127
unsigned shortuint16_t16Unsigned0 .. 65,535
shortint16_t16Signed-32,768 .. 32,767
unsigned intuint32_t32Unsigned0 .. 4,294,967,295
intint32_t32Signed-2,147,483,648 .. 2,147,483,647
unsigned long longuint64_t64Unsigned0 .. 18,446,744,073,709,551,615
long longint64_t64Signed-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807

Floating Point Data Types

C typeIEE754 NameBitsRange
floatSingle Precision32-3.4E38 .. 3.4E38
doubleDouble Precision64-1.7E308 .. 1.7E308

Pointers

The ARMv7-M architecture used in mbed microcontrollers is a 32-bit architecture, so standard C pointers are 32-bits.

Notes

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.

Other Reference