definition of data types uint8_t, uint16_t

NikoTeen

Well-known member
Hi,
for a sketch with Teensy 3.5, developped on Arduino IDE/Teensyduino, I want to use data types uint8_t and uint16_t.

What header file has to be included to know these data types?
 
None, except a sketch typically includes Arduino.h by default in the IDE

This works to build and upload:
Code:
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial && millis() < 4000 );
  Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);

}

uint8_t foo;
uint16_t bar;
void loop() {
  // put your main code here, to run repeatedly:
  foo++;
  if ( foo == 255 ) bar++;
}
 
It might resolve to this a couple of levels down form Arduino.h included includes:

...\hardware\tools\avr\lib\gcc\avr\7.3.0\include\stdint-gcc.h
 
In C and freestanding C, it is provided by <stdint.h>, which is available even when the standard C library is not.
In C++ and freestanding C++, it is provided by <cstdint>, which is available even when the standard C++ library is not.
They are basically provided by the C or C++ compiler.

Like defragster wrote above, they are automagically included by the Arduino environment, so they are always available.


When available, uintN_t are unsigned integers exactly N bits in size with no padding bits (and can therefore represent integers between 0 and 2N-1, inclusive). Similarly, intN_t are signed integers exactly N bits in size using two's complement format where the value of the most significant bit is negative, and the range an N-bit signed integer can represent is from -2N-1 to 2N-1-1, inclusive.

The uint_fastN_t and int_fastN_t types are particularly useful, because they provide at least the same range as an N-bit unsigned or signed integer, repectively, but using the "fastest" type on the current architecture. For example, on 32-bit architectures int_fast16_t is usually 32-bit, since handling exactly 16-bit values tends to require extra instructions (clearing or sign-extending the extra bits).
Furthermore, after including the proper header, these are always available for at least N=8, 16, 32, and 64 –– even when uint32_t etc. are not!
(But that only happens on some DSPs and oddball historical hardware architectures. On AVR and ARM cores, and in general anything supported by the Arduino or PlatformIO environments, you can expect all these to be available.)

Byte order of these types is not specified, but GCC and Clang do provide __BYTE_ORDER__ and __FLOAT_WORD_ORDER preprocessor macros you can check in the code if it actually matters.

It is very much recommended to use the exact-size integers in interchange formats (when transferring data, or implementing file formats), and the fast types for loops and local variables.
 
Back
Top