r/cprogramming • u/JayDeesus • Mar 30 '26
What exactly is inline
I’m coming back to C after a while and honestly I feel like inline is a keyword that I have not found a concrete answer as to what its actual purpose is in C.
When I first learned c I learned that inline is a hint to the compiler to inline the function to avoid overhead from adding another stack frame.
I also heard mixed things about how modern day compilers, inline behaves like in cpp where it allows for multiple of the same definitions but requires a separate not inline definition as well.
And then I also hear that inline is pointless in c because without static it’s broke but with static it’s useless.
What is the actual real purpose of inline? I can never seem to find one answer
10
u/pskocik Mar 31 '26
Maybe just read https://port70.net/~nsz/c/c11/n1570.html#6.7.4 . It's about as long as this question.
On the purpose of inline it says:
Inlines are about being as fast as macros without the pitfalls of macros (like double evaluations of arguments or the inability to "return" a value from a more complex macro without the statement expresion ({ }) extension).
Personally, I like to force this by never using non-static inlines. I always use static inline and with the the gnu attribute ((always_inline,flatten)). But non-static inlines and letting the compiler make inlining decisions for you works too. Just keep in mind that non-static inlines behave differently in C and C++. In C they need one explicit instantiation in some .c file in your project.
12
u/RadiatingLight Mar 30 '26
In very simple terms:
Instead of actually creating a function and calling it (which involves some small amount of overhead from shuffling registers and passing arguments and whatnot), inlined functions are basically pasted directly into the call site.
this causes your code size to increase, and gives a slight speed up. if the function being inlined is very small, it's probably worth it. if the function being inlined is very large, it's probably not worth it.
2
u/flyingron Mar 31 '26
The only practical meaning of inline in current times is that it allows multiple definitions of the function (as long as they are all the same).
2
u/manrussell Mar 31 '26
We use several different vendour embedded compilers for our C code, and the only way we can guarantee that something is inlined, for all compilers, is to use function like macros.
1
u/dontgonmyprofile Mar 31 '26
Inline suggests the compiler to instead of calling to that function it essentially directly copy and pastes the code in that function to wherever you call it
It's just a hint to the compiler and the compiler may choose to not inline it or the compiler may even inline non-inline functions too
1
u/CreepyValuable Apr 01 '26
Inline is like a function is kind of treated more like a macro and plopped straight in where it's called, instead of actually being called and returned from elsewhere in memory.
1
u/Dry-War7589 Apr 01 '26
When you call a function, any arguments and the return address needs to be pushed to the stack. If the function is small, this may introduce unnecessary overhead. Now what inlining does is it instead of pushing everything to the stack, it copies the code of the function to every place it is called. This is faster to execute because there is no need to jump to another function, but if used incorrectly it will increase the file size. Hopefully i didnt overcomplicate the explanation.
1
u/Dontezuma1 Apr 04 '26 edited Apr 04 '26
I don’t think it does anything anymore. In the early days it was a hint to the compiler to remove a function call. But it could be ignored.
I want to say it’s picked up a new meaning since but it’s not critical if you are learning. Compilers are smart about inlining and don’t depend on the keyword.
I had to look up the “new” usage. It will allow you to have multiple functions by the same name as long as they are in separate compilation units without a link error. Kind of the opposite of extern I suppose. Not sure it’s new but it’s the remaining use. If you are new you might not be writing programs that span multiple files so it’s of no use to you.
0
u/[deleted] Mar 31 '26
[deleted] — view deleted comment