inline function in C

In this blog post, you will learn the inline function in C and its concept. You will learn how to use inline function specifiers with function and their effect on them. We also see some programming examples to understand the inline function specifiers. So first let’s understand what is an inline function.

 

What is the inline function in C?

A function declared with an inline function specifier is an inline function. The C language is supporting the inline keyword since C99. The inline specifier is a hint given to the compiler to perform an optimization. The compiler has the freedom to ignore this request.

C standard states that “Making a function an inline function suggests that calls to the function be as fast as possible”. The extent to which such suggestions are effective is implementation-defined. Let’s see an example.

inline void Swap(int *a, int *b)
{
    int tmp= *a;
    *a= *b;
    *b = tmp;
}

 

If the compiler inlines the function, it replaces every call of that function with the actual body (without generating a call). This avoids extra overhead created by the function call. But it may result in a larger executable as the code for the function has to be repeated multiple times. The result is similar to function-like macros.

 

Important points related to inline function in C:

Any function with internal linkage can be an inline function. See the below example in which I have used the swap function that has internal linkage.

static inline void Swap(int *a, int *b)
{
    int tmp= *a;
    *a= *b;
    *b = tmp;
}

 

If a non-static function is declared inline, then it must be defined in the same translation unit. The inline definition that does not use extern is not externally visible and does not prevent other translation units from defining the same function. Let’s see an example ( Compiler: -std=C99).

Example 1:

/*main.c*/
#include<stdio.h>

inline void ok();

int main()
{
    ok();
    
    return 0;
}




/*test.c*/
inline void ok()
{
    //function body
}

Output: Error

 

Example 2:

/*main.c*/
#include<stdio.h>

void ok();

int main()
{
    ok();
    return 0;
}



/*test.c*/
#include<stdio.h>

extern inline void ok()
{
    printf("%d",10);
}

Output: 10

 

Now I am going to explain a very important concept. As we know that An inline definition does not provide an external definition for the function, and does not forbid an external definition in another translation unit.

So you can say that an inline definition provides an alternative to an external definition, which a translator may use to implement any call to the function in the same translation unit. It is unspecified whether a call to the function uses the inline definition or the external definition.

Let’s see an example (Compiler: -std=C99).

Example 1:

Creating an inline function name and the definition is an “inline definition”. Also, I am not creating any external definition of the name

#include<stdio.h>

inline const char *name()
{
    return "Aticle";
}


int main()
{
    printf("%s", name());

    return 0;
}

Output:

 

Note:  According to the C standard, If an identifier declared with internal linkage is used in an expression (other than as a part of the operand of a sizeof or _Alignof operator whose result is an integer constant), there shall be exactly one external definition for the identifier in the translation unit.

Example 2:

Now creating an external definition to the function “name”.

/*main.c*/
#include<stdio.h>

inline const char *name()
{
    return "Aticle";
}


int main()
{
    printf("%s", name());
    
    return 0;
}



/*test.c*/
extern const char *name()
{
    return "world";
}

Output: Unspecified (Could call external or internal definition).

 

You also need to remember that a non-static inline function (function with external linkage) must not contain a definition of a non-const object with static or thread storage duration. Also, it must not contain a reference to an identifier with internal linkage. Consider the below example,

static int data;

inline void fun()
{
    static int n = 1; // error: non-const static in a
    //  non-static inline function

    int value = data; // error: non-static inline
    // function accesses a static variable
}

 

 

Recommended Post

Leave a Reply

Your email address will not be published. Required fields are marked *