在没有括号的if语句中调用函数

Jel*_*tra 7 c syntax

Fedora Linux上最新版本的gcc和clang编译以下程序时没有错误:

#include <ctype.h>
#include <stdio.h>

int main(int argc, char *argv[]) {
    char c = 'a';
    if islower(c)
        printf("%d", c);
    else
        printf("%c", c);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是使用gcc 4.7.2和clang 3.0.相比之下,在我的Mac上,gcc 4.2.1和Apple clang 4.1都抱怨"if islower(c)"行中的括号丢失,正如预期的那样.在所有情况下,我使用"-std = c99"运行编译器.

这是最近版本的gcc和clang中的一个错误,C语言中的怪癖还是其他什么?C99标准(http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf第133页)似乎在所有情况下都要求使用括号.

unw*_*ind 13

它可能islower()是一个宏,并且扩展添加了括号.

发布GCC的预处理输出,您可以通过编译-E选项获得.


Kri*_*ina 10

我只是查看了ctype.h位于的文件/usr/include/ctype.h,找到了以下定义islower:

#define islower(c)  __isctype((c), _ISlower)
Run Code Online (Sandbox Code Playgroud)

__isctype()去找我的定义:

#define __isctype(c, type) \
  ((*__ctype_b_loc())[(int) (c)] & (unsigned short int) type)
Run Code Online (Sandbox Code Playgroud)

所以你的代码if islower(c)扩展为:

if ((*__ctype_b_loc())[(int) (c)] & (unsigned short int) _ISlower)
Run Code Online (Sandbox Code Playgroud)

正如放松所说,在扩张过程中添加了括号.