What does “NPY_INLINE” mean in the numpy library?

Solution for What does “NPY_INLINE” mean in the numpy library?
is Given Below:

I’m trying to define a custom type for numpy in C, and first want to understand the code. Looking at the numpy Github repository, a lot of C functions have “NPY_INLINE” in the definition. What does this do exactly in the code?

For example:

static NPY_INLINE int
npy_is_aligned(const void * p, const npy_uintp alignment)


Its definition can be found in the npy_common.h header file.

// these comments are added by me.

// check if the compiler is MSVC.
#if defined(_MSC_VER)
        // use the vendor-specific keyword, modern
        // versions of MSVC also support inline.
        #define NPY_INLINE __inline

// check if the compiler supports the GNU C extensions,
// that includes for example GCC and Clang among others.
#elif defined(__GNUC__)
    // check if the compiler expects strictly C89.
    #if defined(__STRICT_ANSI__)
         // use the vendor-specific keyword.
         #define NPY_INLINE __inline__
         // if not strictly C89, use standard keyword.
         #define NPY_INLINE inline
    // if it can't be sure, it simply doesn't try.
    #define NPY_INLINE

what this does is attempt to add the inline specifier to a function definition in a C version and compiler agnostic way as the inline function specifier (6.7.4) was only added in C99. this is a suggestion to the compiler that this function should be inlined. the compiler can choose whether it should respect it.

as for what inline functions are: it is the equivalent of you copy-pasting the content of your function into where it would be called. this removes the overhead of calling the function but has other downsides. for more complete information on its effects, usage and history, wikipedia is always lovely.

have fun coding! 🙂