How to define a terabyte constant in C

2 Answers

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
 
#define TERABYTE (1ULL << 40)   // 1 TB in binary (2^40 bytes)
 
int main() {
    unsigned long long size = TERABYTE;
 
    printf("%llu bytes\n", size);
 
    setlocale(LC_NUMERIC, "");  // enable system locale
    printf("%'zu bytes\n", size);
    
    return 0;
}
 
 
 
/*
run:
 
1099511627776 bytes
1,099,511,627,776 bytes
 
*/



 



answered 7 hours ago by avibootz
edited 7 hours ago by avibootz
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() {
    const unsigned long long TERABYTE = 1ULL << 40;
     
    unsigned long long size = TERABYTE;
 
    printf("%llu bytes\n", size);
 
    setlocale(LC_NUMERIC, "");  // enable system locale
    printf("%'zu bytes\n", size);
    
    return 0;
}
 
 
 
/*
run:
 
1099511627776 bytes
1,099,511,627,776 bytes
 
*/


 



answered 7 hours ago by avibootz
edited 7 hours ago by avibootz

Related questions

...