我正在尝试学习一些基本的 C 代码,以增强我的一般技能。我写了一个基本脚本来告诉我数字是大于还是小于 10:
#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;
}
然而当我编译它时,我收到一个错误,指出我正在尝试比较一个指针和一个整数:
dave@dave-[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:
dave@dave-[laptop]:~/Code/C/Examples$ ./004----takeandif
Enter an integer
2
Integer entered by you is 2
2 is greater than 10.
其他答案均不适用于我的情况。我应该改变什么?
最佳答案
替换
if ( "%d" > 10)
与
if( number > 10 )
关于C 错误 : comparison between pointer and integer [enabled by default],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34914445/