Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: definition of data types uint8_t, uint16_t

  1. #1

    definition of data types uint8_t, uint16_t

    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?

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,358
    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++;
    }

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,358
    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\s tdint-gcc.h

  4. #4
    Senior Member
    Join Date
    Feb 2015
    Location
    Finland
    Posts
    270
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •