How to set a buffer size to 2 terabytes in C

1 Answer

0 votes
// A terabyte represent as a number of bytes using a large integer constant
// (usually long long or size_t) 

// A terabyte is:
// 1 TB = 1,099,511,627,776 bytes (binary, 2⁴⁰)
// 1 TB = 1,000,000,000,000 bytes (decimal, SI)

#include <stdio.h>
#include <locale.h>  // Required for setlocale and LC_NUMERIC

int main() {
    size_t size = 2 * (size_t)(1ULL << 40);
    
    printf("size = %zu bytes\n", size);
    
    setlocale(LC_NUMERIC, "");  // enable system locale
    printf("size = %'zu bytes\n", size);

    return 0;
}



/*
run:

size = 2199023255552 bytes
size = 2,199,023,255,552 bytes

*/

 



answered May 2 by avibootz
edited May 2 by avibootz
...