0

What are the data sizes of all the primitive datatypes on a Metro M4 Express SAMD51 C++ implementation? In some C++ implementations, int has 16 bits and in other implementations it has 32 bits. Is

1
  • 1
    It depends on compiler's data model for target processor. But I'd rather use int16_t/int32_t instead. It's much better for portability. – KIIV Oct 28 '19 at 19:40
0

And that is the only difference in fundamental type sizes. On 8-bit Arduinos int is 16 bit and on 32-bit int is 32 bit.

  • short is 16 bit
  • long is 32 bit
  • long long is 64 bit

https://en.cppreference.com/w/cpp/language/types

0

Here is a sketch to quickly find out unsigned maximum values on any architecture and deduce the bit width from them (I had to use sprintf, because Serial.println(unsigned long long) did not work on my Due):

#include <limits.h>

#define sprintfln(args...) { char _qsx6_[200]; snprintf(_qsx6_, 200, args); Serial.println(_qsx6_); } // _qsx6_ may collide with local var

void setup()
{
    Serial.begin(115200);

    unsigned char  uc = 0; uc=~uc; Serial.println(uc);
    unsigned short us = 0; us=~us; Serial.println(us);
    unsigned int   ui = 0; ui=~ui; Serial.println(ui);
    unsigned long  ul = 0; ul=~ul; Serial.println(ul);
    unsigned long long ull = 0; ull=~ull; // doesn't work: Serial.println(~ull);
    
    sprintfln("char:%u short:%u int:%u long:%lu long long:%llu", uc, us, ui, ul, ull);
    sprintfln("char:%u short:%u int:%u long:%lu long long:%llu", UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, ULLONG_MAX);
}

On Arduino Due this gives (I expect it to be the same on SAMD51):

char:255 
short:65535 
int:4294967295 
long:4294967295 
long long:18446744073709551615

Usually you could include "limits.h", but it seems that unsigned long long is missing (at least on Due). Hmmm, I didn't expect int and long to be the same, good to know.

1
  • 1. Note that unsigned int ui = -1; is a valid way to set an unsigned number to it's maximum value. 2. sizeof(int) may be more convenient. 3. int and long are both 32 bits on most 32-bit environments, and even on 64-bit Windows. 4. It would be cool to have also the size of pointers. – Edgar Bonet 2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.