【问题标题】:Having trouble finding what is causing this error无法找到导致此错误的原因
【发布时间】:2016-02-08 21:01:38
【问题描述】:

我正在尝试创建一个程序,该程序将输出 16 个数字的二进制代码。这是我目前所拥有的:

#include <stdio.h>
#include <stdlib.h>
int i;
int count;
int mask;

int i = 0xF5A2;
int mask = 0x8000;
int main()
{
printf("Hex Value= %x Binary= \n", i);
{
    for (count=0; count<15; count++1)
    {
        if (i&mask)
            printf("1\n");
        else
            printf("0\n");
    }
    (mask = mask>>1);
}
return 0;
}

错误:

|16|error: expected ')' before numeric constant|

如果我有任何其他错误,请告诉我,提前谢谢!

【问题讨论】:

  • 哇,我很抱歉它在数字常量之前是“预期的')'”
  • count++1 --> count++。也 (mask = mask&gt;&gt;1); 进入 for 循环
  • 好的,现在没有错误,但它只给了我 16 个 1,没有二进制代码或任何东西
  • count&lt;15 --> count&lt;16

标签: c binary constants codeblocks numeric


【解决方案1】:

错误是指这个表达式:

count++1

这毫无意义。

我假设你想要:

count++ 

划线

for (count=0; count<15; count++)

您的代码中还有其他奇怪之处,例如:

int i;              // Declare an integer named "i"
int mask;           // Declare an integer named "mask"

int i = 0xF5A2;     // Declare another integer also named "i".  Did you forget about the first one???
int mask = 0x8000;  // Did you forget you already declared an integer named "mask"?

printf("Hex Value= %x Binary= \n", i);
{
    [...]
}  // Why did you put a bracket-scope under a PRINTF call?
   // Scopes typically follow loops and if-statements!

 (mask = mask>>1);  // Why put parens around a plain-old expression??


修复代码中的怪异之处后,它应该如下所示:
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i = 0xF5A2;
    int mask = 0x8000;

    printf("Hex Value= %x Binary= \n", i);
    for (int count=0; count<15; ++count, mask>>=1)
    {
        printf("%d\n", (i&mask)? 1 : 0);
    }
    return 0;
}

【讨论】:

    猜你喜欢
    • 2021-08-14
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多