Hello everybody,
Years ago I opened the topic linked below about the same thing but at that time I had much less understanding of C and the answers seemed to me enough. Now I want to learn a little bit more detail. (Honestly I am writing this briefing as I am concerned to be caught and ragged by Salem)
Inline keyword
As far as I understand the inline keyword is just a suggestion to the compiler and the compiler may ignore it. When I declare the function with the inline keyword and define the function below the main function it does not compile. Therefore I got two suggestions from two people with great knowledge:
laserlight: Ditch the prototype and just use the function definition as a declaration.
oogabooga: To avoid some other complexities, you may also want to make it static (i.e., "internal" instead of "external"). [Also c99tutorial suggested the same thing.]
What I understand from the previous discussion and my research.
1) laserlight's suggestion works. If the compiler accepts to inline the definition is there, if it ignores it the decleration is there so it can be linked.
2) If I do the same as 1 but define the function below main function and do not declare the function prototype the code compiles with a warning ("implicit decleration of the function").
3)To resolve this if I type the prototype with the keyword inline it does not work ("undefined reference to the function"). I suspect the the compiler rejects inlining the function and cannot find the function definition to link. Why does not that work?
4)When I remove the inline keyword from the function prototype it works without any warnings or errors. Is it thr right way to use inline functions?
5)If I add static keyword both to the definition and the prototype it works too. As far as I know the static keyword makes the function is known only in the file. Why is this usually recommended to use with inline?
6)If I type the function prototype with inline but the definition without inline it works without any warnings and errors. My guess is that inlining is never suggested to the compiler at that case. But I would expect at least a warning. Why does this seem legit?
P.S. : I use GCC as my compiler with Code::Blocks.