C Data Types

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





8 comments:

05 May 2011

Where on the mbed web site can I find the various header files such as stdint.h, mbed.h, Serial.h etc?

I cannot be the only person who has searched (in vain) for them. Could Dan/Simon put a link to them on a handbook page? Links to the compiler and library documentation on the ARM web site would also be useful.

05 May 2011

Hi Paul

You can find (some of) them from the compiler by clicking on the mbed library in a program, then over on the right hand side click on Summary under Library Details and then "Open Library Page".

For rev 28 for example, this takes you to http://mbed.org/projects/libraries/svn/mbed/trunk?rev=28

Regards
Daniel

05 May 2011

Hi Daniel,

Many thanks for this. All the mbed-specific header files seem to be there.

I guess the standard C/C++ header files are elsewhere. Does anybody know where?

Regards,
Paul

05 May 2011

The standard headers are just that - standard. Their actual content doesn't matter much as long as you know that the compiler supports all the standard C and C++ features.

09 May 2011

I wanted to see what pseudo-types (such as uint32_t) were available and how they were defined. Sure, I could Google it, but looking at the relevant header file seemed a good way of answering the question.

Have you never wanted to "look under the hood" in order to gain understanding of a subject?

16 Aug 2012

Hmm. I just got tricked while doing some fixed-point stuff.... I thought there was a long as in standard C, 64 bits. Just checked, and for whatever reason, long is only 32 bit. To get 64 bit ints you have to used long long... I only figured it out because I tested my code in Xcode.

11 Nov 2012

It would be *really* handy to have access to the versions of the standard libraries that are actually used in the mbed (as requested above). For example: mbed has char = unsigned. MS Visual Studio has char = signed. The standard says that char is implementation specific so really everyone must *always* specify the signedness of char or cast appropriately. I.e. saying "char str[2];" is asking for trouble. It should be "signed char" or "unsigned char" or "int8_t" or "uint8_t".

11 Nov 2012

From the link above:

Quote:

The ARM ABI defines char as an unsigned byte, and this is the interpretation used by the C++ libraries supplied with RVCT.

unsigned char is pretty much default on ARM. I'm not sure how knowing the library versions would help you there. The default compiler switches would be nice, yes.

Posting new comments for this page has been disabled