I was trying to learn some basic c code, to bolster my general skills. I wrote a basic script that tells me if a number is greater or less than 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;
}
And yet when I compile it, I get an error stating that I'm trying to compare a pointer and an integer:
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)
^
And when I run it, it says that all numbers are less than 10:
dave@dave-[laptop]:~/Code/C/Examples$ ./004----takeandif
Enter an integer
2
Integer entered by you is 2
2 is greater than 10.
None of the other answers apply to my situation. What should I change?
Replace
if ( "%d" > 10)
With
if( number > 10 )
我非常确定%d只是一个占位符,并且由于您想将“ number”的值与10进行比较,因此应写成“ if(number> 10)”而不是“ if(”%d“> 10 )”
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.