# 64 bit integer types and printing

**URL:** <https://community.particle.io/t/64-bit-integer-types-and-printing/39126>\
**Category:** Firmware\
**Created:** [January 31, 2018, 8:40pm UTC](https://community.particle.io/t/64-bit-integer-types-and-printing/39126 "2018-01-31T20:40:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaza\_tom](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jaza_tom/32/11323_2.png) [@jaza\_tom](https://community.particle.io/u/jaza_tom)\
**Post date:** [January 31, 2018, 8:40pm UTC](https://community.particle.io/t/64-bit-integer-types-and-printing/39126/1 "2018-01-31T20:40:39Z")

</div>

Hey all.

I’m trying to print the amount of free memory available on my SD card using the SDFat library.

Here a code snippet:

```
uint64_t JazaSD::freeSpace(){

   unsigned int clusterSize = 512L*sd.vol()->blocksPerCluster();
   Serial.printlnf("Cluster Size: %u bytes", clusterSize);

   int freeClusters = sd.vol()->freeClusterCount();
   if(freeClusters < 0) return 0;
   Serial.printlnf("FreeClusters: %d", freeClusters);

   uint64_t freeClusters64 = (uint64_t)(freeClusters);
   uint64_t freeSpace = (uint64_t)(clusterSize) * freeClusters64;
   Serial.printlnf("Free space: %u", freeSpace);
   Serial.printlnf("Free space: %lu", freeSpace);
   Serial.printlnf("Free space: %llu", freeSpace);
   Serial.printlnf("Free space (MB): %lu", freeSpace / 1024);
   Serial.printlnf("Free space (GB): %lu", freeSpace / 1048576L);
   Serial.printlnf("Free space: %f", freeSpace/1073741824L);
   return freeSpace ;
}

```

and here is what the output is when I run that code:

```
Cluster Size: 32768 bytes
FreeClusters: 491247
Free space: 3
Free space: 3
Free space: lu
Free space (MB): 0
Free space (GB): 0
Free space: 0.000000

```

What am I missing? Is there some special trick to printing 64-bit numbers?

If printing 64-bit numbers is not possible, how can I confirm that I have the correct number stored in my `freeSpace` variable?

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [January 31, 2018, 9:19pm UTC](https://community.particle.io/t/64-bit-integer-types-and-printing/39126/2 "2018-01-31T21:19:29Z")

</div>

I don’t think the Arduino print code knows about 64-bit integers at all. I would try switching to `snprintf()` instead.

---

<div class="post-metadata">

**Author:** ![jaza\_tom](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jaza_tom/32/11323_2.png) [@jaza\_tom](https://community.particle.io/u/jaza_tom)\
**Post date:** [February 1, 2018, 3:04pm UTC](https://community.particle.io/t/64-bit-integer-types-and-printing/39126/3 "2018-02-01T15:04:08Z")

</div>

Thanks for the response @bko here is my modified code.

It still doesn’t work.

```
   char tempBuf[100];

   unsigned int clusterSize = 512L*sd.vol()->blocksPerCluster();
   Serial.printlnf("Cluster Size: %u bytes", clusterSize);

   int freeClusters = sd.vol()->freeClusterCount();
   if(freeClusters < 0) return 0;
   Serial.printlnf("FreeClusters: %d", freeClusters);

   uint64_t freeSpace64 = (uint64_t)(clusterSize) * (uint64_t)(freeClusters);

   Serial.println("freeSpace64 using \"llu\" specifier:");
   snprintf(tempBuf, 100, "%llu", freeSpace64);
   Serial.println(tempBuf);
   Serial.println("freeSpace64 using \"lu\" specifier:");
   snprintf(tempBuf, 100, "%lu", freeSpace64);
   Serial.println(tempBuf);
   Serial.println("freeSpace64 using \"u\" specifier:");
   snprintf(tempBuf, 100, "%u", freeSpace64);
   Serial.println(tempBuf);

   Serial.println("freeSpace64 using printing tricks with \"lu\" specifier: ");
   snprintf(tempBuf, 100, "%0lu", freeSpace64/1000000UL);
   Serial.print(tempBuf);
   snprintf(tempBuf, 100, "%0lu", freeSpace64%1000000UL);
   Serial.println(tempBuf);
   Serial.println("freeSpace64 using printing tricks with \"llu\" specifier: ");
   snprintf(tempBuf, 100, "%0llu", freeSpace64/1000000UL);
   Serial.print(tempBuf);
   snprintf(tempBuf, 100, "%0llu", freeSpace64%1000000UL);
   Serial.println(tempBuf);

```

\_  
\_  
produces this output:  
\_

```
Cluster Size: 32768 bytes
FreeClusters: 491317
freeSpace64 using "llu" specifier:
lu
freeSpace64 using "lu" specifier:
129
freeSpace64 using "u" specifier:
129
freeSpace64 using printing tricks with "lu" specifier:
00
freeSpace64 using printing tricks with "llu" specifier:
lulu
```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [February 1, 2018, 3:30pm UTC](https://community.particle.io/t/64-bit-integer-types-and-printing/39126/4 "2018-02-01T15:30:50Z")

</div>

There was a discussion about `long long` a long time ago and I guess things haven't changed since

> [@Problems when using long long](https://community.particle.io/t/problems-when-using-long-long/19302/16):
>
> My guess is that the sprintf is not handling the 64-bt datatype. %ld means long not long long. To print, I see you have 2 options: write your own routine to print the long long value convert to double and print using sprintf print out in hex, first the upper 32-bits, then the lower 32. I hope that helps.

---

<div class="post-metadata">

**Author:** ![jaza\_tom](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jaza_tom/32/11323_2.png) [@jaza\_tom](https://community.particle.io/u/jaza_tom)\
**Post date:** [February 1, 2018, 3:32pm UTC](https://community.particle.io/t/64-bit-integer-types-and-printing/39126/5 "2018-02-01T15:32:58Z")

</div>

I’ve decided I don’t need too much precision here in the end.

So instead of counting the free bytes on the SD card, I’m comfortable just counting the free KBs, which can be stored in a 32-bit data type.

Thanks for the responses.

This will continue to haunt my dreams I’m sure but for now its not mission critical for me.

---

<div class="post-metadata">

**Author:** ![drowning\_ophelia](https://avatars.discourse-cdn.com/v4/letter/d/cc9497/32.png) [@drowning\_ophelia](https://community.particle.io/u/drowning_ophelia)\
**Post date:** [September 22, 2021, 6:30am UTC](https://community.particle.io/t/64-bit-integer-types-and-printing/39126/6 "2021-09-22T06:30:12Z")

</div>

The Arduino print() and println() functions only support up to 32-bit types, so I created a modified version of the Arduino Print.h & Print.cpp files with overloads that accept 64-bit integer (signed long long int, unsigned long long int, int64\_t, uint64\_t) and long double types. They’re available here: [Arduino/hardware/avr at master · Michael-Brodsky/Arduino · GitHub](https://github.com/Michael-Brodsky/Arduino/tree/master/hardware/avr). You’ll need to copy them to your Arduino install folder (mine’s at C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino - or just search for the files under your Arduino folder) and replace the old files there (make a backup first!!). Cheers!
