"C Language Practice every day (21)" Inline function

Source: Internet
Author: User

Foreword: When calling a function, it usually takes some time to make a call, pass the parameters, jump to the function code, and return, and the solution of the C language is to use the class function macro. In C99, the second method is proposed: inline function.

inline function: Turning a function into an inline function will suggest that the compiler call the function as fast as possible, as far as the proposed effect is defined by the implementation. Therefore, making a function inline may simplify the function's invocation mechanism, but it may not work. Inline functions are implemented by compilers, and macros are replaced when precompiled.

Create an inline function method: Use the function specifier inline in a function declaration.


Features of the inline function:

1, similar to Macro, when the compiler sees an inline function declaration, it replaces the function call with the function body when the inline function is used, and its effect is as if the code for the body of the function is typed here. such as:

Source:

#include <stdio.h>inline void Eatline ()//the definition of inline function {while (GetChar ()! = ' \ n ') continue;} int main () {eatline ();//Function Call}
The actual effect of compiling with the compiler is as follows:

#include <stdio.h>inline void Eatline ()//the definition of inline function {while (GetChar ()! = ' \ n ') continue;} int main () {while (GetChar ()! = ' \ n ') continue;}


2, the inline function does not have a separate block of code reserved for it, so the address of the inline function cannot be obtained.

3 . Inline functions are not displayed in the debugger. For example, even if the above function is compiled with the GCC-G option, there will be no eatline function when debugging through GDB.

4, the inline function should be relatively short. For very long functions, calling a function is less than the time it takes to run the function body, and it does not save much time to use inline functions.

5, the definition of the inline function and the call to the function must be in the same file, that is, inline functions have internal links. In multiple file programs, each file that invokes an inline function is defined for that function. An easy way to achieve this is to define an inline function in the header file and include the header file in the file that uses the function.

6, unlike C + + , C agrees to mix inline function definitions with external function definitions. Since the defined inline function can only be used in this file, the external function defined can be externally declared through extern and used in other files. such as:

File1.cinline Double Square (double);d ouble Square (double x) {return x * x;} File2.cextern Double Square (double);d ouble Square (double x) {return x * x;} File3.cextern Double Square (double); int main () {double kw = Square (w);}

The double function used in file1.c is the inline function defined in this file, while the double function used in file2.c and file3.c does file2.c the external function defined in the.





"C Language Practice every day (21)" Inline function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.