1

我試圖學習一些基本的C代碼,以增強我的一般技能。我寫的,告訴我,如果一個數字是大於或小於10一個基本的腳本:C錯誤:指針和整數之間的比較[默認情況下啓用]

#include <stdio.h> 
#include <unistd.h> 
#include <string.h> 


int main() 
{ 
    int number; 

    printf("Enter an integer\n"); 

    scanf("%d",&number); 

    printf("Integer entered by you is %d\n", number); 

    if ("%d" > 10) 
     printf("%d is greater than 10.\n", number); 
    else 
     printf("%d is less than 10.\n", number); 

    return 0; 
} 

然而,當我編譯它,我得到一個錯誤,說明我試圖比較一個指針和一個整數:

[email protected][laptop]:~/Code/C/Examples$ gcc 004----takeandif.c -o 004----takeandif 
004----takeandif.c: In function ‘main’: 
004----takeandif.c:16:14: warning: comparison between pointer and integer [enabled by default] 
    if ("%d" > 10) 
      ^

當我運行它,它說,所有的數字都低於10:

[email protected][laptop]:~/Code/C/Examples$ ./004----takeandif 
Enter an integer 
2 
Integer entered by you is 2 
2 is greater than 10. 

其他答案都不適用於我的情況。我應該改變什麼?

+1

'if(「%d」> 10)'你認爲那是什麼? – John3136

回答

1

更換

if ("%d" > 10) 

隨着

if(number > 10) 
+0

現在它工作正常。感謝您的幫助,我被困在那裏。 – Rich

0

我敢肯定,%d只是一個佔位符,因爲你要「數」的值與10,你應該寫「如果(數字> 10)」而不是「如果(」%d「> 10)」

相關問題