×

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

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

Exponential() in C

Introduction: In C language, there are available many types of functions. The exponential() function is one of them. It is a mathematical function. This article describes using the pow() property to...

3 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Addition of Matrix in C

The mathematical operation of includingor greater matrices is called the addition of matrices. A matrix is a square series of numbers, symbols, words, letters, and different things. In this article,...

3 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array. To determine the Mean: To get the average, mean is determined. The formula...

2 minutes read.

Floyd’s triangle in C

Floyd’s triangle in C Floyd’s triangle is named after a person Robert Floyd. It is a right-angled triangle that is of natural numbers starting from 1 and consecutively selects the upcoming...

4 minutes read.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Bar3d() function in C Graphics

The bar3d function is used to create a 2-dimensional filled-in rectangular bar. We can also create three-dimensional shapes in C using helpful function. The first step in creating this 3D...

3 minutes read.

How to open a C file on android mobile

A C file is a file that has a .c extension and contains code written in the C language. C is a computer programming language developed by Dennis Ritchie at...

4 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.