about #ifndef and macro_function

129 Views Asked by At

i have changed complier from msvc to mingw,

macro function : _countof was not inluded in mingw <stblib.h> or <stdio.h>

#define _countof(array) (sizeof(array) / sizeof(array[0]))

maybe it's only for msvc,

so,i have to add the defination into header file myself,

i'm not sure about the code below:

#ifndef _countof(array)
#define _countof(array) (sizeof(array) / sizeof(array[0]))
#endif

It seems that this way of writing is not common.

1

There are 1 best solutions below

0
On BEST ANSWER

The underscore prefix indicates that it is proprietary and not and ISO standard name.

It is the macro name that is tested in #ifndef, so just:

#ifndef _countof
    #define _countof(array) (sizeof(array) / sizeof(array[0]))
#endif

Though personally I prefer:

#if !defined _countof
    #define _countof(array) (sizeof(array) / sizeof(*array))
#endif

Also a macro is just a macro. There is no such thing as a macro function, rather you might refer to a function-like macro, being a macro that takes arguments, but even then semantically they are very different from functions.