我正在尝试学习一些基本的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.
其他答案都不适用于我的情况。我应该改变什么?
答案 0 :(得分:1)
替换
if ( "%d" > 10)
使用
if( number > 10 )
答案 1 :(得分:0)
我非常确定%d只是一个占位符,因为你想比较&#34;数字&#34;到10,你应该写&#34; if(number&gt; 10)&#34;而不是&#34; if(&#34;%d&#34;&gt; 10)&#34;