Is scientific notation safe for integer constants in C?

前端 未结 6 817
心在旅途
心在旅途 2021-02-03 18:04

For a while, I\'ve been representing large powers of 10 in constants using scientific notation, just so I don\'t have to count the zeros. e.g.

#define DELAY_USE         


        
6条回答
  •  萌比男神i
    2021-02-03 18:38

    You want to use user defined literals:

    constexpr long long operator "" _k(long long l) {
        return l * 1000;
    }
    
    constexpr long long operator "" _m(long long l) {
        return l * 1000 * 1000;
    }
    

    then you can simple do:

    long long delay = 1_m;
    long long wait = 45_k;
    

提交回复
热议问题