Static Functions

Normal functions (also known as non-static functions) and static functions have different visibility and linkage characteristics.

❶ Normal Functions

  1. Duration: refers to the lifetime of a function or variable, which is the period during which the function or variable exists in memory.
    • Static Duration:
      • Normal functions have static duration. This means that once they are defined, they exist for the entire runtime of the program. They are available for the time the program starts until it terminates. They are not created and destroyed during function calls but are always present in memory throughout the program’s execution.
  2. Scope: refers to the region of the program where a function or variable can be accessed.
    • File | Global Scope:
      • Normal functions have file scope (also known as global scope) by default. This means they are accessible from anywhere within the same file. If they are declared in a header file and that header file is included in multiple source files, they are accessible across those files. However, the function definition itself should be in exactly one source file to avoid multiple definition errors.
  3. Linkage: refers to the visibility of a function or variable across different translation units (source files).
    • External Linkage:
      • Normal functions (by default) have external linkage. This means they are visible outside the file in which they are declared.
      • They can be accessed from other files if the function is declared with the extern keyword or if the function prototype (declaration) is included in a header file and that header file is included in other source files.

Example of Normal Function

Below are two ways of accessing the normal function in other non-declaration files, first by using extern keyword, then by using header file in which have declaration of the function.

1️⃣ Using Extern Keyword:

file1.c:

#include <stdio.h>

void normalFunction() {  // Normal function with external linkage
    printf("This is a normal function.\n");
}

file2.c:

#include <stdio.h>

extern void normalFunction();  // Declaration of the function from file1.c

int main() {
    normalFunction();  // Calls normalFunction defined in file1.c
    return 0;
}

2️⃣ Using Header File:

Header File (common.h):

#ifndef COMMON_H
#define COMMON_H

void normalFunction();  // Function declaration

#endif

file1.c (with header file):

#include "common.h"
#include <stdio.h>

void normalFunction() {  // Definition of the function
    printf("This is a normal function.\n");
}

file2.c (with header file):

#include "common.h"

int main() {
    normalFunction();  // Calls normalFunction defined in file1.c
    return 0;
}

❷ Static Function

  1. Duration: refers to the lifetime of a function or variable, which is the period during which the function or variable exists in memory.
    • Static Duration:
      • Static functions have static duration. This means that they exist for the entire lifetime of the program, just like normal functions. They are available from the time the program starts until it terminates. They are not created and destroyed during function calls but are always present in memory throughout the program’s execution.
  2. Scope: refers to the region of the program where a function or variable can be accessed.
    • File | Global Scope:
      • Static functions have file scope. This means they are only accessible within the source file where they are defined. They cannot be called from other source files, even if the function is declared in a header file included in those other files.
  3. Linkage:  refers to the visibility of a function or variable across different translation units (source files).
    • Internal Linkage:
      • Static functions have internal linkage. This means they are not visible outside the file in which they are defined. The static keyword restricts their visibility to the translation unit (source file), preventing access from other files.
      • This is the main difference between the normal function and static function.

Example of Static Function

file1.c:

#include <stdio.h>

static void staticFunction() {  // Static function with internal linkage
    printf("This is a static function in file1.c\n");
}

void publicFunction() {
    staticFunction();  // Calls staticFunction within the same file
}

file2.c:

#include <stdio.h>

// Declaration of the function from file1.c
void publicFunction();

int main() {
    publicFunction();  // Calls publicFunction defined in file1.c
    // staticFunction();  // Error: staticFunction is not visible in file2.c
    return 0;
}

Table

Function TypeDurationScopeLinkage
Normal FunctionStaticFile/GlobalExternal
Static FunctionStaticFile/GlobalInternal