A funny thing with sprintf

后端 未结 6 2024
伪装坚强ぢ
伪装坚强ぢ 2020-12-15 14:17

I\'m so confused with sprintf that a funny problem with different platfrom. Code :

int main () 
{
    char sql[1024];
    uint32_t app_id = 32;
    uint64_t          


        
6条回答
  •  轮回少年
    2020-12-15 14:38

    Use %llu format string for task_id in sprintf()as follows:

    sprintf(sql, "%llu, %u", task_id, app_id);
    //             ^
    //            for: long long unsigned int
    

    Edit: As @Simonc suggested its better to use: PRIu32 and PRIu64 macros defined in (as you have Linux tag) do like:

    sprintf(sql, "%"PRIu64", %"PRIu32"", task_id, app_id);
    //               ^           ^
    //       for:   uint64_t    uint32_t  
    

提交回复
热议问题