Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

C Functions Categories

Functions in C can be categorized into several types based on their characteristics and purpose.

Here are some common categories of functions:

1. Standard Library Functions:

  • These are functions provided by the C standard library, such as <stdio.h>,<stdlib.h>,<string.h> etc.
  • Standard library functions serve various purposes, including input/output operations, memory management, string manipulation, mathematical calculations, and more.
  • Examples: printf(), scanf(), malloc(), strlen(), strcmp(), etc.

2. User-Defined Functions:

  • User-defined functions are created by the programmer to perform specific tasks within a program.
  • These functions provide modularity, code reusability, and abstraction, making the program easier to understand and maintain.
  • Examples: Functions that perform calculations, validate input, process data, implement algorithms, etc.

3. Recursive Functions:

  • Recursive functions are functions that call themselves directly or indirectly.
  • They are useful for solving problems that can be divided into smaller sub-problems of the same type.
  • Recursive functions have a base case that defines the termination condition, and a recursive case that calls the function with a smaller input.
  • Examples: Calculating factorials, computing Fibonacci series, traversing data structures recursively, etc.
  • Example of recursive functions:
C
#include <stdio.h>

int sum(int n) {
    // Base case: if n is 1, return 1
    if (n == 1) {
        return 1;
    }

    // Recursive case: add n to the sum of numbers from 1 to (n-1)
    return n + sum(n - 1);
}

int main() {
    int num = 5;
    int result = sum(num);
    printf("The sum of numbers from 1 to %d is: %d\n", num, result);
    return 0;
}

4. Library Functions:

  • Library functions are functions grouped together in a library for specific purposes.
  • Libraries contain related functions that can be reused across multiple programs.
  • These functions provide additional functionality beyond what is available in the standard library.
  • Examples: Math library functions (), time-related functions (), graphics functions (), etc.

5. Callback Functions:

  • Callback functions are functions that are passed as arguments to other functions.
  • The receiving function can call the callback function at a specific point, allowing for customization and extensibility.
  • Callback functions are commonly used in event-driven programming or when implementing data structures like sorting algorithms.
  • Examples: Event handlers, comparison functions for sorting, callback functions in APIs, etc.

6. Inline Functions:

  • Inline functions are small functions that are expanded at the call site instead of being called like regular functions.
  • The purpose of inline functions is to avoid the overhead of function calls and improve performance.
  • Inline functions are defined using the inline keyword.
  • Examples: Small utility functions, simple arithmetic operations, etc.
  • Example of inline function:
C
#include <stdio.h>

// Inline function to calculate the square of a number
inline int square(int num) {
    return num * num;
}

int main() {
    int num = 5;
    int result = square(num);
    printf("The square of %d is: %d\n", num, result);
    return 0;
}