VisualC++ 6.0 with unsigned long long and sprintf

核能气质少年 提交于 2019-12-05 01:45:29

问题


I want to sprintf() an unsigned long long value in visual C++ 6.0 (plain C).

char buf[1000]; //bad coding 
unsigned __int64 l = 12345678; 
char t1[6] = "test1"; 
char t2[6] = "test2"; 
sprintf(buf, "%lli, %s, %s", l, t1, t2);

gives the result

12345678, (null), test1

(watch that test2 is not printed)

and l = 123456789012345 it gives an exception handle

any suggestions?


回答1:


To print an unsigned __int64 value in Visual C++ 6.0 you should use %I64u, not %lli (refer to this page on MSDN). %lli is only supported in Visual Studio 2005 and later versions. So, your code should be:

sprintf(buf, "%I64u, %s, %s", l, t1, t2);



回答2:


printf uses the ellipsis to pass a variable argument list. The (null) you see is the second part of your long long, which happen to be all 0 bits. Set it to 1<<60+1<<30 and you'll get a crash as the 1<<60 part is interpreted as a char*.

The correct solution would be to break down the number in three parts of 10 digits, "verylongvalue % 10000000000" "(verylongvalue/10000000000) % 10000000000" "verylongvalue/100000000000000000000".




回答3:


Apparently, you did not assign additionaltext to the necessary char * (string). Note that the long int was processed, the comma was copied and only the %s generated (null).



来源:https://stackoverflow.com/questions/200676/visualc-6-0-with-unsigned-long-long-and-sprintf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!