Jelle Zijlstra
Jelle Zijlstra

Reputation: 340

Function call in if statement without parentheses

Recent versions of gcc and clang on Fedora Linux compile the following program without error:

#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;
}

This is with gcc 4.7.2 and clang 3.0. On my Mac, in contrast, both gcc 4.2.1 and Apple clang 4.1 complain about missing parentheses in the "if islower(c)" line, as expected. In all cases, I ran the compilers with "-std=c99".

Is this a bug in recent versions of gcc and clang, a quirk in the C language, or something else? The C99 standard (http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf p. 133) appears to mandate parentheses around if expressions in all cases.

Upvotes: 7

Views: 1374

Answers (2)

Kristina
Kristina

Reputation: 903

I just looked through the ctype.h file located in /usr/include/ctype.h and found the following definition for islower:

#define islower(c)  __isctype((c), _ISlower)

Going to the definition for __isctype() I find:

#define __isctype(c, type) \
  ((*__ctype_b_loc())[(int) (c)] & (unsigned short int) type)

So your code if islower(c) expands to:

if ((*__ctype_b_loc())[(int) (c)] & (unsigned short int) _ISlower)

Which, as unwind said, added the parenthesis during expansion.

Upvotes: 10

unwind
unwind

Reputation: 399803

It's likely that islower() is a macro, and that the expansion adds the parenthesis.

Post the pre-processed output from GCC, which you can get by compiling with the -E option.

Upvotes: 13

Related Questions