2017-10-11 66 views
2

我真的很新的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

回答

3

您不能對printf語句使用單引號。試試這個:

printf("%d", j); 
2

'%d'是一個多字符常量,因爲你已經用單引號字符多個字符。它的值是實現定義的,但C標準堅持認爲它是一個int類型。 (因此編譯器診斷爲「來自整數的指針」)。

你想用"%d"來代替,就是用雙引號字符。

printfconst char*指針作爲第一個參數。形式上"%d"const char[3]類型,但通過稱爲指針衰減的機制它成爲該第一個參數的合適值。

相關問題