“printf”式时将整数指针不进行强制转换

问题描述:

我真的很新的C和我试图运行下面的代码在C:“printf”式时将整数指针不进行强制转换

#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, ...); 

我不知道这里有什么问题。任何帮助?

+6

单引号。使用双引号表示字符串“%d”。 – phoxis

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

printf("%d", j); 

'%d'是一个多字符常量,因为你已经用单引号字符多个字符。它的值是实现定义的,但C标准坚持认为它是一个int类型。 (因此编译器诊断为“来自整数的指针”)。

你想用"%d"来代替,就是用双引号字符。

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