How to convert unsigned long to string

后端 未结 6 1891
[愿得一人]
[愿得一人] 2020-12-08 03:14

In the C language, how do I convert unsigned long value to a string (char *) and keep my source code portable or just recompile it to work

6条回答
  •  春和景丽
    2020-12-08 03:36

    Try using sprintf:

    unsigned long x=1000000;
    char buffer[21];
    sprintf(buffer,"%lu", x);
    

    Edit:

    Notice that you have to allocate a buffer in advance, and have no idea how long the numbers will actually be when you do so. I'm assuming 32bit longs, which can produce numbers as big as 10 digits.

    See Carl Smotricz's answer for a better explanation of the issues involved.

提交回复
热议问题