【问题标题】:error: format ‘%lu’ expects a matching ‘long unsigned int’ argument [-Werror=format=]错误:格式‘%lu’需要一个匹配的‘long unsigned int’参数[-Werror=format=]
【发布时间】:2018-11-24 11:21:38
【问题描述】:

我该如何解决error?。在网上搜索了很多,但没有发现任何类似的问题。

使用 gcc -Wall -Werror -std=c99 编译 错误代码:

NameOfProgram: In function ‘main’:
NameOfProgram:17:23: error: format ‘%lu’ expects a matching ‘long unsigned int’ argument [-Werror=format=]
             printf("%lu , number");
                     ~~^
NameOfProgram:21:23: error: format ‘%lu’ expects a matching ‘long unsigned int’ argument [-Werror=format=]
             printf("%lu , number");

c99 中的程序:

#include <stdio.h>
#include <stdlib.h>



int main(void) {

unsigned long number;
printf("Geben Sie irgendeine natürliche Zahl ein, die größer als 0 ist: ");
scanf(" %lu", &number);
printf("\n");

while(number > 0) {

    if(number % 2 == 0) {
        number = number / 2;
        printf("%lu , number");
    }
    else {
        number = (3 * number) + 1;
        printf("%lu , number");
    }
}


return EXIT_SUCCESS;
}

【问题讨论】:

  • 查看printf 调用中字符串的语法高亮。论据在哪里?它应该在 inside 字符串中吗?

标签: c c99


【解决方案1】:

您的引文放错地方了。试试这个

printf("%lu", number);

但作为旁注,可能值得考虑更便携的 PRI 表示法。例如,如果您有一个显式的 32 位无符号整数,则打印格式字符串因编译器而异:

uint32_t number;
printf("My value is: %"PRIu32, number);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2014-01-28
    相关资源
    最近更新 更多