Iam programing in C. I want some description about static and inline functions. I know that if we make a function static then it is an indication to compiler that it is under internal linkage for only one translation unit.
I have following doubts regarding static and inline :
- If we make a function as static , can we use it in other translation units …i.e in other .c files? If yes ..how?
- If we make the function as static inline what is the difference then ? How it will be treated by the compiler ?
- Does making a function as static gives the same effect as a macro?
- Treating a function to be inline is dependent on the compiler depending on the size . So is there any way that we can forcefully make it to be treated like inline?
- How we can use inline and static functions for optimization ?
Please shed some light on the above things inline.
Platform is Linux, gcc compiler , C language.
A
staticfunction can’t be used in other translation units. That’s their raison d’être.inlinehints to the compiler that the function should be inlined instead of called.Making a function static is different than using a macro. A macro essentially overrules the compiler. Whether it thinks it wise to inline or not, a macro will be inlined; macros are textual substitution. You can also pass a
staticfunction to something requiring a function pointer. Can’t do that with a macro.Macros will forcefully inline anything. Even specifying
inlinecan be overruled.Make functions you don’t want to export
static. If a function is really small, and you really think it should be inlined, you can tell the compiler that withinline. Macros are really only for metaprogramming. The compiler knows better than you.