C prog错误:int前的预期表达式

时间:2022-02-26 22:45:23

This is the program:

这是这个项目:

#include <stdio.h>
#define round(a) ((a-0.5)<int(a))?int(a):int(a+1)
int main() {
double a = 5.2;
int m = round(a);
printf("%d", m); }

and it shows the error: expected expression before 'int'

它显示了错误:在'int'之前的预期表达式

4 个解决方案

#1


2  

round is a name reserved by the standard C library so it is undefined behaviour to call your macro that name (even if you don't include math.h).

round是由标准C库保留的名称,因此调用该名称的宏是未定义的行为(即使不包括math.h)。

Your algorithm could be better expressed like this:

你的算法可以这样表达:

#define my_round(a)  ( (int)((a) + 0.5) )

which also has the benefit of only evaluating its argument once.

它的好处是只对它的论点进行一次评估。

It would be preferable to use an inline function:

最好使用内联函数:

inline int my_round(double d)
{
    return d + 0.5;    
}

Note that both options cause undefined behaviour if a is outside the bounds of INT_MIN, INT_MAX roughly . If it's in a critical environment you should make your inline function check the bounds of d before doing the conversion to int.

注意,如果a超出INT_MIN的范围,那么这两个选项都会导致未定义的行为。如果它是在一个关键的环境中,你应该让你的内联函数在进行到int的转换之前检查d的边界。

#2


0  

This

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

Has the brackets in the wron places

wron的括号有吗

Should be

应该是

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

#3


0  

The problem is that int(a) is not valid C.

问题是int(a)不是有效的C。

Redefine your macro as follows:

重新定义您的宏如下:

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

Note that I've also added parentheses around a in (a)-0.5.

注意,我还在(a)-0.5中添加了圆括号。

P.S. What's the reason for making it a macro and not, say, a function?

那么,为什么要让它是一个宏而不是一个函数呢?

#4


0  

The error is because of int(a). Syntactically it is wrong. It should be (int)(a).

这个错误是因为int(a)。在语法上是错误的。应该是(int)(a)。

#1


2  

round is a name reserved by the standard C library so it is undefined behaviour to call your macro that name (even if you don't include math.h).

round是由标准C库保留的名称,因此调用该名称的宏是未定义的行为(即使不包括math.h)。

Your algorithm could be better expressed like this:

你的算法可以这样表达:

#define my_round(a)  ( (int)((a) + 0.5) )

which also has the benefit of only evaluating its argument once.

它的好处是只对它的论点进行一次评估。

It would be preferable to use an inline function:

最好使用内联函数:

inline int my_round(double d)
{
    return d + 0.5;    
}

Note that both options cause undefined behaviour if a is outside the bounds of INT_MIN, INT_MAX roughly . If it's in a critical environment you should make your inline function check the bounds of d before doing the conversion to int.

注意,如果a超出INT_MIN的范围,那么这两个选项都会导致未定义的行为。如果它是在一个关键的环境中,你应该让你的内联函数在进行到int的转换之前检查d的边界。

#2


0  

This

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

Has the brackets in the wron places

wron的括号有吗

Should be

应该是

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

#3


0  

The problem is that int(a) is not valid C.

问题是int(a)不是有效的C。

Redefine your macro as follows:

重新定义您的宏如下:

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

Note that I've also added parentheses around a in (a)-0.5.

注意,我还在(a)-0.5中添加了圆括号。

P.S. What's the reason for making it a macro and not, say, a function?

那么,为什么要让它是一个宏而不是一个函数呢?

#4


0  

The error is because of int(a). Syntactically it is wrong. It should be (int)(a).

这个错误是因为int(a)。在语法上是错误的。应该是(int)(a)。