C Inline Functions

Optimizing function calls for better performance

⚡ What are Inline Functions?

Inline functions are a compiler optimization hint that suggests replacing function calls with the actual function code. This eliminates function call overhead, making your program faster for small, frequently-used functions.


// Simple inline function example
inline int square(int x) {
    return x * x;
}

int main() {
    int result = square(5);  // Compiler may replace with: int result = 5 * 5;
    printf("Square of 5 is %d\n", result);
    return 0;
}
                                    

Output:

Square of 5 is 25

Key Inline Function Concepts

🚀

Performance

Eliminates function call overhead

inline int max(int a, int b) {
    return (a > b) ? a : b;
}
💡

Compiler Hint

Suggests inlining, not mandatory

// Compiler decides whether to inline
inline double celsius_to_fahrenheit(double c) {
    return (c * 9.0/5.0) + 32.0;
}
📏

Small Functions

Best for short, simple functions

inline int is_even(int n) {
    return n % 2 == 0;
}
⚠️

Trade-offs

May increase code size

// Use sparingly for large functions
inline void complex_calculation() {
    // Large function body...
}

🔹 Basic Inline Function Example

inline functions replace function calls with actual code, reducing function call overhead. When the compiler encounters an inline function call, it substitutes the function body directly at that location. This optimization speeds up frequently called small functions by eliminating call overhead. For example, calculating circle area or finding minimum values becomes faster with inline functions. Modern compilers often inline automatically, but explicit inline keywords suggest optimization hints to compilers.

#include <stdio.h>

// Inline function for calculating area of circle
inline double circle_area(double radius) {
    return 3.14159 * radius * radius;
}

// Inline function for minimum of two numbers
inline int min(int a, int b) {
    return (a < b) ? a : b;
}

int main() {
    double radius = 5.0;
    double area = circle_area(radius);
    
    int x = 10, y = 20;
    int minimum = min(x, y);
    
    printf("Circle area: %.2f\n", area);
    printf("Minimum of %d and %d: %d\n", x, y, minimum);
    
    return 0;
}

Output:

Circle area: 78.54
Minimum of 10 and 20: 10

🔹 When to Use Inline Functions

Inline functions work best for small, frequently called functions with simple logic. Good candidates include mathematical operations, accessors, and utility functions. Avoid inlining complex functions, recursive functions, or those called rarely. Excessive inlining increases code size, potentially degrading performance through cache misses. Let modern compilers make inlining decisions automatically using -O2 or -O3 optimization flags. Balance performance gains against potential code bloat carefully.

✅ Good candidates for inlining:

  • Small functions: 1-3 lines of code
  • Frequently called: Used in loops or hot paths
  • Simple operations: Math calculations, comparisons
  • Accessor functions: Getting/setting values

❌ Avoid inlining for:

  • Large functions: Complex logic or many statements
  • Recursive functions: Can cause infinite expansion
  • Functions with loops: May increase code size significantly
// Good inline candidate
inline int abs_value(int x) {
    return (x < 0) ? -x : x;
}

// Poor inline candidate (too complex)
inline void sort_array(int arr[], int n) {
    // Complex sorting algorithm...
    for (int i = 0; i < n-1; i++) {
        for (int j = 0; j < n-i-1; j++) {
            if (arr[j] > arr[j+1]) {
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}

🧠 Test Your Knowledge

What is the main benefit of inline functions?