'printf' 从整数创建指针而不进行强制转换

'printf' makes pointer from integer without a cast

我真的是 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, ...);

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

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

printf("%d", j);

'%d' 是一个多字符文字,因为您在单引号字符中包含了多个字符。它的值是实现定义的,但 C 标准坚持认为它是 int 类型。 (因此编译器诊断 "pointer from an integer")。

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

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