×

Inline Function in C

Inline Function in C

A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where definitions are small and substituted when the function call occurs. Function substitution is totally a compiler's choice. The property of the inline function is that the compiler inserts the entire body of the function in the place where inline function name is used in the program.

The main reason for making a function inline is to hint the compiler that it is worth making extra effort to call a function faster than otherwise; generally, by substituting the function's code into its caller. It eliminates the need for a call and a return request sequence and allows the compiler to perform certain optimizations between the bodies of both functions. Sometimes it will be necessary for the compiler to emit a stand-alone copy of an object code for the function even if it is an inline function, such as if it is necessary to take the address of a function or it cannot be inlined in a particular context or if the optimization has been turned off.

An inline function of any kind can emit stand-alone abject code, or not emit stand-alone object code, or only emit stand-alone object code if it is required. Sometimes an inline function can lead to duplication of object code and will become a massive problem for some reasons such as:

  • It leads to wastage of space.
  • It can cause the pointers to compare one another, which may or may not be equal.
  • It might reduce the effectiveness of the instruction cache. However, inlining might do the same in different ways too.

Syntax:

             inline function_name ()
             {
                         //function definition
             } 

E.g.:

 #include <stdio.h>
 #include <conio.h>
 inline int mul (int a, int b) //inline function declaration
 {
 return (a * b);
 }
 int main ()
 {
 int p;
 p = mul(3 , 6);
 printf (“ The product of the numbers = %d \n”, p);
 return 0;
 } 

Output:

The product of the numbers = 18

Inline rules:

  • This is a function where all the declaration, including the definition, mentions inline and never mentions an extern. There has to be a definition in the same translation unit. The C programming standard refers to this as an inline definition. A program can have a separate definition that is not an inline function in the unit, and the compiler can choose either that or the inline function. Functions may not contain modifiable static variables and may not refer to static functions or variables where they are declared.

E.g.:

             inline int mul (int a, int b);  //inline declaration
             inline int min (int a, int b) // inline definition
             {         
                         return a > b ? a : b;
             } 

            Here, the function will not be callable from other files.

  • The function where atleast one declaration is inline, but other declaration does not mention inline or extern. There must be a definition in the translation unit. It works as a normal function and can be allied from other translation units present in the program.

E.g.:

                         extern inline int max (int a, int b); //extern and inline declaration
                         inline int min (int a, int b) // inline definition
             {         
                         return a > b ? a : b;
             }
             int mul (int a, int b);  //no mention of inline declaration 

Here, the functions are callable from other files.

  • The function is said to be a static inline. A local definition may be emitted if and only if required. A programmer can have multiple definitions in the program, but still, it works in the same way. Inlining a function reduces the program to a portable one. This will be useful primarily for the small function that uses macros. If the function is not always inlined, then you might get duplicates of the object code. Hence the solution for this will be including static inline function in either of the header file id they are to be widely used or just in the source file that uses them.

E.g.:

             static inline int min (int a, int b) // static inline definition
             {         
                         return a > b ? a : b;
             }
 Consider another instance;
 #include <stdio.h>
 #include <conio.h>
 static inline int num() //inline function
 {
 return 2;
 }
 int main()
 {
 int number;
 number = num(); //inline function call
 printf (“ The number returned using inline function is = %d \n” ,number);
 return 0;
 } 

Output:

The number returned using inline function is = 2

Advantages:

  • Inline functions can include multiple lines of code without trailing backslashes.
  • Debugging a code is accessible in inline when compared to macros.
  • The main advantage of using an inline function is that it does not require the function call and does not return anything from the function.
  • They have their scope of variables and can return a value.
  • Since inline are functions, the compiler's type of arguments is checked whether they are right or wrong.
  • No risk is involved in it is called multiple times. But it might be present in macros which is dangerous when the argument is an expression.

Disadvantages:

  • It increases the file size just as much as the function code is copied repeatedly in the program whenever it is called.s

Related Topics

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.