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'
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).
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.
This
#define round(a) ((a-0.5)<int(a))?int(a):int(a+1) Has the brackets in the wron places
Should be
#define round(a) (((int)((a)-0.5))<(a))?(int)(a):(int)(a+1) (a) in the comparison, otherwise it will always be true.
(int)(a), notint(a)return 0;at the end.#include <stdlib.h>and usereturn EXIT_SUCCESS;, which is clearer than "0".