0

I am just confused by the inline keyword in C Programming language.

  1. In my practice, an inline function has internal scope, so how does it differ with the static keyword?

  2. inline function has internal scope, but I saw some inline function plus the static keyword — what`s the use?

//temp.c
inline void exit_aio(){
    puts("inline");
}

void test(){
    exit_aio();//error,undefined reference to `exit_aio'
}

If I add the static keyword in the definition of function exit_aio(), then the program will work normally.

7
  • Best to make sure your code sample compiles cleanly. minimal reproducible example For now it is at least missing #include files. Commented Oct 27, 2024 at 19:22
  • 1
    Can't reproduce (after adding #include <stdio.h> and a main() function). Commented Oct 27, 2024 at 19:22
  • 1
    @WeatherVane, call function test in the main function Commented Oct 27, 2024 at 19:46
  • This is a good site, you can read about inline link Commented Oct 27, 2024 at 19:50
  • 1
    See Is "inline" without "static" or "extern" ever useful in C99?. There are numerous other questions about inline functions in C. Note that inline is a hint; the compiler can ignore the hint — and can inline functions not explicitly marked inline. As a simple rule of thumb, make functions static inline in a header and include the header where you use the function. Commented Oct 28, 2024 at 1:52

1 Answer 1

0
  1. An inline function has internal scope, but I saw some inline function plus the static keyword — what`s the use?

It is to place inline functions in headers without risking linker problems.

I am just confused by the inline keyword

Modern compilers take it only as a hint and may decide whether to inline function or not. If the function is not static and is defined in a header file included by many modules, the linker will complain.

Some compilers have special attributes to force inlining - for example gcc's __attribute__((always_inline))

 //error,undefined reference to `exit_aio'

If I add the static keyword in the definition of function exit_aio(), then the program will work normally.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.