2

In C, if an inline function declared before the main(), but the definition was after the main as follows:

inline int sum(int x, int y);

int main()
{
  int z;
  z= sum(2,2);
}

inline int sum(int x, int y)
{
  return x+y;
}

or if we made the declaration without inline like this:

int sum(int x, int y);

int main()
{
  int z;
  z= sum(2,2);
}

inline int sum(int x, int y)
{
  return x+y;
}

Will the compiler deal with the function in both cases as inline (replacing the function call with the actual function body)?

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Huster
  • 43
  • 2
  • It's implementation-dependent. – Barmar Aug 07 '23 at 19:04
  • What the compiler actually does is (always, by definition) implementation dependent. But your two examples are not semantically equivalent: the behavior of the first is undefined, but the behavior of the second is defined. – John Bollinger Aug 07 '23 at 19:06
  • Unsure of the relevance but C18 §6.7.4.2 for `inline` says "Function specifiers shall be used only in the **declaration** of an identifier for a function" (my bolding). – Weather Vane Aug 07 '23 at 19:07
  • Yes, @WeatherVane, and definitions are a kind of declaration. Or if you prefer, definitions *incorporate* declarations. – John Bollinger Aug 07 '23 at 19:07
  • @JohnBollinger (before your edit) a function definition without a previous function prototype amounts to a declaration as well as a definition. – Weather Vane Aug 07 '23 at 19:08
  • To quote the C11 standard: *"Making a function an inline function suggests that calls to the function be as fast as possible. The extent to which such suggestions are effective is implementation-defined."* Which means that the answer to your question depends on the compiler. You should expect that an `inline` function is no different than any other function, unless you have documentation for your compiler that says otherwise. – user3386109 Aug 07 '23 at 19:09
  • @WeatherVane, per C17 6.7/5: "**A *definition* of an identifier is a declaration for that identifier** that [...]" (emphasis added). So again, a definition is a declaration. Regardless of whether there are any other declarations, or where. – John Bollinger Aug 07 '23 at 19:11
  • 1
    I wonder how a one-pass compiler can make a subsequent function definition inline. – Weather Vane Aug 07 '23 at 19:12
  • @JohnBollinger so long as the declarations match. – Weather Vane Aug 07 '23 at 19:14
  • Whether or not they match, @WeatherVane. But if they are not compatible then the program has UB. – John Bollinger Aug 07 '23 at 19:14
  • @JohnBollinger surely the compiler will object with an error. – Weather Vane Aug 07 '23 at 19:15
  • @WeatherVane, the compiler *may* reject the code if two declarations of the same identifier are not compatible -- but again, UB. However, "compatible" is not the same as "identical". It is allowed for some object or function attributes to differ between declarations of an identifier without affecting compatibility, and in particular, the `inline` attribute is allowed to appear on some declarations of a function and not other declarations of the same in the same scope. – John Bollinger Aug 07 '23 at 19:19
  • 3
    Hustler, Do yourself a favor and pretend you never heard of `inline`. It does not do what you think it does, and what it primarily does do -- *hint* to the compiler about making calls to that function fast -- is something compilers don't usually need encouragment for or help with. On the other hand, the rules around `inline`, especially when not combined with `static`, set you up for surprising and unnecessary errors. – John Bollinger Aug 07 '23 at 19:25

0 Answers0