【问题标题】:'printf' makes pointer from integer without a cast'printf' 从整数生成指针而不进行强制转换
【发布时间】:2017-10-11 19:05:15
【问题描述】:

我是真正的 C 新手,我正在尝试在 C 中运行以下代码:

#include <stdio.h>
int main()
{
  unsigned long i = 1UL << 2;
  int j = (i==4);
  printf('%d', j);
  return 0;
}

但它给出了错误:

prog.c: In function 'main':
prog.c:6:10: warning: multi-character character constant [-Wmultichar]
   printf('%d', j);
          ^
prog.c:6:10: warning: passing argument 1 of 'printf' makes pointer from integer without a cast [-Wint-conversion]
In file included from prog.c:1:0:
/usr/include/stdio.h:362:12: note: expected 'const char * restrict' but argument is of type 'int'
 extern int printf (const char *__restrict __format, ...);

我不确定这里出了什么问题。有什么帮助吗?

【问题讨论】:

  • 单引号。对字符串“%d”使用双引号。

标签: c printf unsigned


【解决方案1】:

您不能在 printf 语句中使用单引号。试试这个:

printf("%d", j);

【讨论】:

    【解决方案2】:

    '%d' 是一种多字符文字,因为您在单引号字符中包含了多个字符。它的值是实现定义的,但 C 标准坚持它是一个 int 类型。 (因此编译器诊断“来自整数的指针”)。

    您想要"%d",即使用双引号字符。

    printfconst char* 指针作为第一个参数。形式上,"%d"const char[3] 类型,但通过称为指针衰减的机制,它成为第一个参数的合适值。

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 2023-02-08
      • 2022-01-05
      • 2012-06-09
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      相关资源
      最近更新 更多