function-like macro versus macros

后端 未结 1 339
盖世英雄少女心
盖世英雄少女心 2021-01-22 07:31
gcc (GCC) 4.7.2
c89

Hello,

I have been looking at a test suite and I noticed this function-like macro declared like this:

#defi         


        
相关标签:
1条回答
  • 2021-01-22 07:47

    There's one difference between the object-like and function-like macros:

    #define OBJECT     char *msg1 = NULL
    #define FUNCTION() char *msg2 = NULL
    
    void somefunc(void)
    {
        int OBJECT = 5;
        int FUNCTION = 10;
        ...
    }
    

    The declaration for OBJECT is replaced by the macro (so the code won't compile), but the reference to FUNCTION is not a macro invocation because it is not followed by an open parenthesis.

    This is seldom important. However, when it is, it really matters.

    A more typical case might be a function that can be implemented as a macro. For sake of discussion (because it is easily understood rather than because it is a good example):

    extern int isdigit(int c);
    #define isdigit(c) ((c) >= '0' && (c) <= '9')
    

    and in an implementation file:

    int (isdigit)(int c)
    {
        assert((c >= 0 && c <= UCHAR_MAX) || c == EOF);
        return isdigit(c);
    }
    

    Ignoring little details like that isn't how isdigit() is likely to be implemented, and a macro implementation of isdigit() is not allowed to evaluate its argument more than once, and you aren't supposed to redefine things that are in the standard C library, the function definition is not macro expanded because the name isdigit is not followed by (, but the macro inside the function is expanded. At least the function is implemented in terms of the macro, which pretty much guarantees the same behaviour.

    0 讨论(0)
提交回复
热议问题