C错误:“{”令牌前的预期表达式

时间:2022-12-03 22:42:45

This is the code http://pastebin.com/Y7zQkVHZ

这是代码http://pastebin.com/Y7zQkVHZ

and the compiler gives me the above error. What is the mistake ? thanks(:

编译器给出了上面的错误。出了什么错?谢谢(:

Here is the code in an immediately readable form:

以下是一份可立即阅读的代码:

#include <stdio.h>
#define round(a) {    \
    if(a < 0)    \
        ((a + 0.5) < (int)a)? (int)a: (int)a + 1;    \
    else        \
        ((a - 0.5) < (int)a)? (int)a: (int)a + 1;    \
}

#define ARRLENG 8 /* array length */

/* function prototype */
void arrRounder(double[ARRLENG]);

int main()
{
    double arr[ARRLENG] = {2.13, 6.9, 9.49999, 0.5, 8.0, 0, -2.4, -2.5};
    arrRounder(arr);
    return 0;
}

void arrRounder(double arr[ARRLENG])
{
    int i = 0;

    for(i = 0; i < ARRLENG; i++)
        arr[i] = round(arr[i]);

    for(i = 0; i < ARRLENG; i++)
        printf("%lf\n", arr[i]);
}

5 个解决方案

#1


5  

Your round() macro is indeed a mixture of statement and expression, which is not permissible in this way.

您的round()宏实际上是语句和表达式的混合,这样是不允许的。

Either you should transform the if into another ternary operator - which makes the whole stuff even more unreadable - or you should put it into a function.

你可以把if转换成另一个三元运算符——这会使整个东西更加不可读——或者你可以把它转换成一个函数。

#2


3  

The preprocessor replaces rounder with the macro body verbatim. So you will have a line that looks like

预处理器用宏体逐字替换了rounder。所以这条线是这样的

arr[i] = { ... };

That's not a valid C statement.

这不是一个有效的C语句。

Macro invocations might look like function calls, but they aren't.

宏调用可能看起来像函数调用,但实际上不是。

#3


1  

Assuming that your current rounding algorithm is sound, this does the same thing in expression form:

假设您当前的舍入算法是合理的,那么它的表达式形式也是一样的:

#define round(a) (((a) + ((a) < 0 ? 0.5 : -0.5) < (int)(a))? (int)(a): (int)(a) + 1)

Note all the extra parentheses around a, in case it's an unparenthesized expression. This is best (necessary) practice in macros.

注意a周围所有的括号,以防它是一个无括号的表达式。这是宏中最好的(必要的)实践。

#4


0  

#include <stdio.h>
/*macro*/
#define round(a) (((a - (int)a) < 0.5) ? (int)a : (int)(a + 1))
/*const*/
#define ARRLENG 8 /* array length */

/* function prototype */
void arrRounder(double[ARRLENG]);

/*main*/
int main()
{
    double arr[ARRLENG] = {2.13, 6.9, 9.49999, 0.5, 8.0, 0, -2.4, -2.5};
    arrRounder(arr);
    return 0;
}

/*function declaration*/
void arrRounder(double arr[ARRLENG])
{
    int i = 0;

    for(i = 0; i < ARRLENG; i++)
        arr[i] = round(arr[i]);

    for(i = 0; i < ARRLENG; i++)
        printf("%lf\n", arr[i]);
}

I have not tried this code with other numbers but should work fine. Try to find a way to avoid the cast operator (int) because may have impredictable outcomes.

我没有尝试使用其他数字的代码,但是应该可以正常工作。尝试寻找一种方法来避免cast算符(int),因为它可能有不可预测的结果。

#5


-1  

as per me you should use simple braces instead of curley. check out for the Syntax of defining a macro.

就我而言,你应该使用简单的牙套而不是曲线。查看定义宏的语法。

#1


5  

Your round() macro is indeed a mixture of statement and expression, which is not permissible in this way.

您的round()宏实际上是语句和表达式的混合,这样是不允许的。

Either you should transform the if into another ternary operator - which makes the whole stuff even more unreadable - or you should put it into a function.

你可以把if转换成另一个三元运算符——这会使整个东西更加不可读——或者你可以把它转换成一个函数。

#2


3  

The preprocessor replaces rounder with the macro body verbatim. So you will have a line that looks like

预处理器用宏体逐字替换了rounder。所以这条线是这样的

arr[i] = { ... };

That's not a valid C statement.

这不是一个有效的C语句。

Macro invocations might look like function calls, but they aren't.

宏调用可能看起来像函数调用,但实际上不是。

#3


1  

Assuming that your current rounding algorithm is sound, this does the same thing in expression form:

假设您当前的舍入算法是合理的,那么它的表达式形式也是一样的:

#define round(a) (((a) + ((a) < 0 ? 0.5 : -0.5) < (int)(a))? (int)(a): (int)(a) + 1)

Note all the extra parentheses around a, in case it's an unparenthesized expression. This is best (necessary) practice in macros.

注意a周围所有的括号,以防它是一个无括号的表达式。这是宏中最好的(必要的)实践。

#4


0  

#include <stdio.h>
/*macro*/
#define round(a) (((a - (int)a) < 0.5) ? (int)a : (int)(a + 1))
/*const*/
#define ARRLENG 8 /* array length */

/* function prototype */
void arrRounder(double[ARRLENG]);

/*main*/
int main()
{
    double arr[ARRLENG] = {2.13, 6.9, 9.49999, 0.5, 8.0, 0, -2.4, -2.5};
    arrRounder(arr);
    return 0;
}

/*function declaration*/
void arrRounder(double arr[ARRLENG])
{
    int i = 0;

    for(i = 0; i < ARRLENG; i++)
        arr[i] = round(arr[i]);

    for(i = 0; i < ARRLENG; i++)
        printf("%lf\n", arr[i]);
}

I have not tried this code with other numbers but should work fine. Try to find a way to avoid the cast operator (int) because may have impredictable outcomes.

我没有尝试使用其他数字的代码,但是应该可以正常工作。尝试寻找一种方法来避免cast算符(int),因为它可能有不可预测的结果。

#5


-1  

as per me you should use simple braces instead of curley. check out for the Syntax of defining a macro.

就我而言,你应该使用简单的牙套而不是曲线。查看定义宏的语法。