代码之家  ›  专栏  ›  技术社区  ›  Peter Miehle

VisualC++6.0,带无符号长型和sprintf

vc6 c
  •  4
  • Peter Miehle  · 技术社区  · 16 年前

    我想在VisualC++ 6中添加一个未签名的长long值(普通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);
    

    给出结果

    12345678, (null), test1
    

    (看那个 test2 未打印)

    l = 123456789012345 它提供异常处理

    有什么建议吗?

    3 回复  |  直到 6 年前
        1
  •  9
  •   ChrisN    16 年前

    打印一个 unsigned __int64 VisualC++ 6中的值应该使用 %I64u 不是 %lli (参阅 this page 在MSDN上)。 %LLI 仅在Visual Studio 2005和更高版本中受支持。 因此,您的代码应该是:

    sprintf(buf, "%I64u, %s, %s", l, t1, t2);
    
        2
  •  1
  •   MSalters    16 年前

    printf使用省略号传递变量参数列表。您看到的(空值)是长整型的第二部分,恰好是全部0位。将其设置为1<<60+1<<30,则会发生崩溃,因为1<<60部分被解释为char*。

    正确的解决方案是将数字分解为10位数字的三部分,“VerylongValue%1000000000”(VerylongValue/1000000000)%1000000000”、“VerylongValue/100000000000000000”。

        3
  •  -1
  •   gimel    16 年前

    显然,你没有指派 additionaltext 到必要的 char * (字符串)。请注意 long int 已处理,复制了逗号,仅 %s 生成 (null) .

    推荐文章