printf如何输出64位整数测试

来自:http://blog.csdn.net/joeblackzqq/article/details/11522257

关于printf函数输出64位数的问题,其实在window下和linux下是不一样的:

【linux】

printf("%lld/n",a);

printf("%llu/n",a);

【windows】

printf("%I64d/n",a);

printf("%I64u/n",a); 

发现原博主的测试代码不够严谨,所以修改了下,ubuntu测试程序如下:

ubuntu@ubuntu:~/test$ vim test.c
#include <stdio.h>

typedef long long int64;
typedef unsigned long long uint64;

int main ()
{
    int64    c = 0xffffeeeeddddccccLL;        // 有符号    
    uint64   uc = 0xffffeeeeddddccccULL;      // 无符号    

    printf("%lld, %llu\n", c, uc);     // 整数形式输出    
    printf("%llx, %llx\n", c, uc);     // 十六进制格式输出    
    printf("%#llx, %#llx\n", c, uc);   // 带0x的十六进制格式输出    

    return 0;
}
~                                                                                                                                                                                                                                                   
"test.c" 16L, 501C written                                                                               
ubuntu@ubuntu:~/test$ gcc -o demo test.c
ubuntu@ubuntu:~/test$ ./demo 
-18765284782900, 18446725308424768716
ffffeeeeddddcccc, ffffeeeeddddcccc
0xffffeeeeddddcccc, 0xffffeeeeddddcccc




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
分析: 大正整数类LPInteger需要存储一个200位的整数,因此可以使用一个长度为200的整型数组来存储。在输入时需要判断输入的数据是否合法,如果输入的数据中含有非数字的字符,则输出error,否则将数据存储到数组中。在输出时需要从数组的高位到低位遍历数组,将每一位数字输出,同时统计位数。最后输出位数即可。 代码实现: ```c #include <stdio.h> #include <string.h> #define MAX_LEN 200 class LPInteger { private: int num[MAX_LEN]; public: LPInteger() { memset(num, 0, sizeof(num)); } void input() { char str[MAX_LEN + 1]; scanf("%s", str); int len = strlen(str); if (len > MAX_LEN) { printf("error!\n"); return; } for (int i = 0; i < len; i++) { if (str[i] < '0' || str[i] > '9') { printf("error!\n"); return; } num[MAX_LEN - len + i] = str[i] - '0'; } } void output() { int len = MAX_LEN; while (len > 0 && num[len - 1] == 0) { len--; } if (len == 0) { printf("0"); } else { for (int i = len - 1; i >= 0; i--) { printf("%d", num[i]); } } printf("\n%d\n", len); } }; int main() { LPInteger a; a.input(); a.output(); return 0; } ``` 测试样例: 输入: ``` 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 ``` 输出: ``` 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 200 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值