Why does uint64_t and unsigned long long not work?

Hi all,

See this test sketch:

void loop (void) { ; }

void setup (void) {

        int x;

        Serial.begin(115200);

        x = sizeof(unsigned long long);
        Serial.print("Size of unsigned long long is ");
        Serial.println(x);

        x = sizeof(uint64_t);
        Serial.print("Size of uint64_t is ");
        Serial.println(x);

//      unsigned long long y = 0xFFFFFFFFFFFFFFFF;
//      uint64_t z = 0xFFFFFFFFFFFFFFFF;

}

Running it yields:

Size of unsigned long long is 8
Size of uint64_t is 8

Uncommenting the two commented lines, however, generates an error:

test:17: error: integer constant is too large for ‘long’ type
test:18: error: integer constant is too large for ‘long’ type

Anyone know:

(1) Why this happens ...and
(2) If it can be fixed?

Thanks!

-- Roger

Does it work with unsigned long long ? 0xFFFFFFFFFFFFFFFF is indeed too large for long long, 0x7FFFFFFFFFFFFFFF is the max.

Append "LL" or "ULL" depending on target the.

AWOL:
Append "LL" or "ULL" depending on target the.

Well how about that! It works!

Both LL and ULL work.

Now the question is... WHY do I have to use that?

If I declare the variable as "unsigned long long" and then give it an unsigned long long number like 0xFFFFFFFFFFFFFFFF why do I have to append "ULL"?

It makes no sense why I should have to do that.

Oh by the way, thanks for the reply and help. A karma++ to you!

-- Roger

It makes no sense why I should have to do that.

Pick up a good C book and read about "type promotion".

Krupski:
Now the question is... WHY do I have to use that?

The compiler infers the type of the literal constant from its value. I believe the rule for integer values is that if the value will fit in an int the type is int, otherwise the type is long int. If you want it to be any other type, for example long long int, then you have to tell the compiler what type to use.

dhenry:

It makes no sense why I should have to do that.

Pick up a good C book and read about "type promotion".

Found it... "type conversion" in my old dog-eared K&R book. Now I understand. Thank you!

-- Roger