【问题标题】:C Error: comparison between pointer and integer [enabled by default]C 错误:指针和整数之间的比较 [默认启用]
【发布时间】:2016-01-21 02:38:37
【问题描述】:

我试图学习一些基本的 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" &gt; 10) 你觉得有什么作用?

标签: c compilation compiler-errors ubuntu-14.04 compiler-warnings


【解决方案1】:

替换

if ( "%d" > 10)

if( number > 10 )

【讨论】:

  • 现在可以正常工作了。感谢您的帮助,我被困住了。
【解决方案2】:

我很确定 %d 只是一个占位符,因为你想将 "number" 的值与 10 进行比较,你应该写 "if (number > 10)" 而不是 "if ("%d " > 10)"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    相关资源
    最近更新 更多