• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ What is the purpose of the keyword 'inline' in C?

Hilton D

I read multiple Scaler & StackOverflow questions regarding inline in C but am still confused.
  • There is no practical difference between static inline void f(void) and static void f(void).
  • In C, inline void f(void) does not operate as it does in C++. In C, how does it work?
  • What does extern inline void f(void); truly do?
I've never used the inline keyword in my C programs, and when I encounter it in other people's code, it's nearly always static inline, which has no distinction from merely static.


King Coder
Hello there, @Hilton D.

From what I can gather, the purpose of the inline keyword is to simply increase program execution time. I'm using this site as my source for this, so I recommend you read up on it and see if you can understand it better than me - I'm finding it hard to understand myself that I can't even simplify it(note: exhausted by the heat at the moment, so maybe if I get the time when I'm cool and rested, I'll try my best to simplify this for you).

Hope you get something useful from the link!


It's been a while since I used the syntax in anger, but basically....

"inline" is a compiler directive which says, do not vtable or pointer reference the function, do NOT use 'call', instead copy the resulting assembler for the function into "this" function instead. It saves you a TON of processor time. The function call process can be quite lengthy, especially if you are using C++ templates/abstraction/inheritance. Lots of stack push/pop'ing and potentially a page fault or two, resulting in context switches back to the kernel, and some MMU page table reloads etc. etc. etc. Basically a function call can add 100s or 1000s of instructions into the process. That can be avoided if you just "inline" the assembler verbatim. There are obviously caveats and it's use needs clear understanding to avoid unforeseen "scope" related issues and memory mamangement / register use etc.

As I said I haven't used the inline keyword that much lately, but I think you question around "static" and "extern" and more related to forward declarations of function pointers.

To give you an example of where I have seen it used, why and how.

Stock exchange. We received trade messages translated them, checked a few "fat finger checks" and wrote them out "on the wire". A HUGE amount of the critical path was taken up splitting the string into key=>value pairs on a delimiter.

The std lib functions for splitting the string ended up taking up about 50% of the processing time wire to wire. First we inlined some of the functions, removing the stack churn, which sped it up.

Then someone found code in another project which was using an ASM file to do the splitting of the string and leaving behind a lovely 2 dimensional array of key->value pairs.

The ASM went directly to the memory for the string and used CPU extensions to iterate of it moving pointers into the 2D array as it went. Using the CPU's built in iteration functionality gave huge performance gains and the CPU can pipeline, predictive and parallel execute the microcode.

We "extern"'ed the asm entry label. Declared it as an "extern "C" inline" function. The only complicated part, if I recall correctly was passing it the base pointers for the string and output tables, there is some really ugly looking syntax (in GCC) to populate registers before handing over to asm.

The linker takes care of the "extern", but I'm honestly not sure if it's the compiler or the linker that does the static code copy. I suppose that comes down to what executable type you are producing, like ELF of DLL for example.