How to define a petabyte constant in C

1 Answer

0 votes
// A petabyte (binary) is:
              
// 1 PiB = 2⁵⁰ bytes = 1,125,899,906,842,624 bytes
// A petabyte (decimal) is:
// 1 PB = 1,000,000,000,000,000 bytes
 
#include <stdio.h>
#include <locale.h>
 
#define PETABYTE (1ULL << 50)        // 1 PiB (binary petabyte)
#define PETABYTE_DEC 1000000000000000ULL  // 1 PB (decimal)
 
int main(void) {
    unsigned long long size = PETABYTE;
   
    printf("%llu bytes\n", size);
   
    // Enable locale so %'llu prints commas (glibc only)
    setlocale(LC_NUMERIC, "");
 
    printf("%'llu bytes\n", size);
 
    return 0;
}

 
 
/* 
run:
 
1125899906842624 bytes
1,125,899,906,842,624 bytes
 
*/

 



answered 5 hours ago by avibootz
edited 4 hours ago by avibootz

Related questions

...