C 函数,如宏 : expected an expression

标签 c function macros

有人可以帮助我吗,因为我不知道这里的世界发生了什么 所以基本上这就是我正在努力解决这些功能

#define LOWCONVERT(c) (if(isupper(c) > (0) \
                   isupper(c) == (0))

上面的宏应该将大写字母转换为小写字符

用于比较两个数字的第二个宏是:

#define COMPARISON(d,f) (if (d > f))\
                    printf("d is greater than f\n");\
                    else\
                        if (d == f)\
                    printf("d equals f\n");\
                    else\
                        if(d < f)\
                        printf("d is smaller than f\n");)

当我使用它们时,它给了我: “错误:需要一个表达式”

最佳答案

不能将语句用作表达式。如果您想将多个语句分组到一个宏中,常见的方法是使用 do...while 循环:

#define COMPARISON(d, f)                     \
    do {                                     \
        if ((d) > (f))                       \
            printf("d is greater than f\n"); \
        else if ((d) == (f))                 \
            printf("d equals f\n");          \
        else if((d) < (f))                   \
            printf("d is smaller than f\n"); \
    } while (0)

另请注意,我将宏参数的用法放在括号内,否则参数可能包含导致比较操作失败的表达式。

关于C 函数,如宏 : expected an expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21219653/

相关文章:

c++ - PyModule_AddObject 崩溃

macros - 是否可以使用符号宏来获得类似标签的行为?

python - 我如何向 Python 中的列表添加限制或规则?

javascript - 将一个函数传递给另一个函数?

c++ - 从过剩教程中了解宏测量数组计数

c++ - 如何将多参数模板传递给宏?

c - iup 程序在 IupOpen(argc, argv) 中崩溃;

使用字符减法的 C strcmp 实现

c - Linux 内核 2.6 中的计时

c - 函数声明上的 "Error: array type has incomplete element type"