代码之家  ›  专栏  ›  技术社区  ›  andrewrk

如何使用printf格式化无符号long long int?

  •  311
  • andrewrk  · 技术社区  · 16 年前
    #include <stdio.h>
    int main() {
        unsigned long long int num = 285212672; //FYI: fits in 29 bits
        int normalInt = 5;
        printf("My number is %d bytes wide and its value is %ul. A normal number is %d.\n", sizeof(num), num, normalInt);
        return 0;
    }
    

    输出:

    My number is 8 bytes wide and its value is 285212672l. A normal number is 0.
    

    我想这个意外的结果来自于打印 unsigned long long int . 你怎么 printf() 无符号长长整型 ?

    11 回复  |  直到 6 年前
        1
  •  408
  •   rogerdpack    12 年前

    将ll(el el)long long修饰符与u(unsigned)转换一起使用。(在Windows、GNU中工作)。

    printf("%llu", 285212672);
    
        2
  •  79
  •   Nathan Fellman    14 年前

    您可能希望尝试使用inttypes.h库,该库为您提供以下类型: int32_t , int64_t , uint64_t 等。 然后可以使用它的宏,例如:

    uint64_t x;
    uint32_t y;
    
    printf("x: %"PRId64", y: %"PRId32"\n", x, y);
    

    这是“保证”不会给你同样的麻烦 long , unsigned long long 等等,因为您不必猜测每种数据类型中有多少位。

        3
  •  43
  •   HeavenHM    6 年前

    %d ——为 int

    %u ——为 unsigned int

    %ld ——为 long int

    %lu ——为 unsigned long int

    %lld ——为 long long int

    %llu ——为 unsigned long long int

        4
  •  35
  •   Steven    15 年前

    对于使用MSV的长时间(或\u int64),应使用%I64d:

    __int64 a;
    time_t b;
    ...
    fprintf(outFile,"%I64d,%I64d\n",a,b);    //I is capital i
    
        5
  •  32
  •   tofutim    6 年前

    这是因为%llu在Windows下不能正常工作,并且%d不能处理64位整数。我建议使用priu64,你会发现它也可以移植到Linux上。

    试试这个:

    #include <stdio.h>
    #include <inttypes.h>
    
    int main() {
        unsigned long long int num = 285212672; //FYI: fits in 29 bits
        int normalInt = 5;
        /* NOTE: PRIu64 is a preprocessor macro and thus should go outside the quoted string. */
        printf("My number is %d bytes wide and its value is %" PRIu64 ". A normal number is %d.\n", sizeof(num), num, normalInt);
        return 0;
    }
    

    产量

    My number is 8 bytes wide and its value is 285212672. A normal number is 5.
    
        6
  •  11
  •   ST3    9 年前

    在Linux %llu 在窗户里 %I64u

    虽然我发现它在Windows2000中不起作用,但那里似乎有一个bug!

        7
  •  8
  •   Sandy    12 年前

    使用vs2005将其编译为x64:

    %LLU运行良好。

        8
  •  4
  •   sparkes    16 年前

    不规范的事情总是奇怪的。)

    长的部分 在GNU之下 L , ll q

    在窗户下面我相信 陆上通信线 只有

        9
  •  4
  •   kungfooman    9 年前

    十六进制:

    printf("64bit: %llp", 0xffffffffffffffff);
    

    输出:

    64bit: FFFFFFFFFFFFFFFF
    
        10
  •  2
  •   Bernd Elkemann    9 年前

    除了人们多年前写的:

    • 您可能在gcc/mingw上得到这个错误:

    main.c:30:3: warning: unknown conversion type character 'l' in format [-Wformat=]

    printf("%llu\n", k);

    那么您的mingw版本并不默认为c99。添加此编译器标志: -std=c99 .

        11
  •  0
  •   vzczc    12 年前

    一种方法是用vs2008将其编译为x64

    按预期运行:

    int normalInt = 5; 
    unsigned long long int num=285212672;
    printf(
        "My number is %d bytes wide and its value is %ul. 
        A normal number is %d \n", 
        sizeof(num), 
        num, 
        normalInt);
    

    对于32位代码,我们需要使用正确的Int64格式说明符%I64u。因此它变为。

    int normalInt = 5; 
    unsigned __int64 num=285212672;
    printf(
        "My number is %d bytes wide and its value is %I64u. 
        A normal number is %d", 
        sizeof(num),
        num, normalInt);
    

    此代码适用于32位和64位的vs编译器。