Inline Functions in C: Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
m (add another option)
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
Line 7:
 
'''File: example1.h'''
<sourcesyntaxhighlight lang="c">
inline long something(long i)
{
return i + 2;
}
</syntaxhighlight>
</source>
 
'''File: example1.c'''
<sourcesyntaxhighlight lang="c">
#include "example1.h"
extern inline long something(long i);
</syntaxhighlight>
</source>
 
This way, the compiler inlines the function if possible, but additionally allocates a name or reference that points to the specific implementation, in case a call is needed when inlining is not possible.
Line 25:
 
'''File: example2.h'''
<sourcesyntaxhighlight lang="c">
static inline long something(long i)
{
return i + 2;
}
</syntaxhighlight>
</source>
 
This is another solution, adding the static keyword.