ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

C Programming Tutorial• 6 min read

Functions, Prototypes & Storage Classes

Header declarations, pass-by-value, and the static keyword

C requires functions to be declared or defined before they are called. Function prototypes declare the signature in header files (.h), implemented in .c files.

Function Prototypes & Storage Classes

Function Prototype: Informs the compiler of parameter types and return type before the actual implementation is compiled.

static local variables: Retain their value between multiple function invocations across the lifetime of the program, stored in the Data Segment rather than the Call Stack.

static global functions: Restrict visibility strictly to the current translation unit (.c file), acting like a private function in OOP.

Function Prototypes and Static Variables
#include <stdio.h>

// Function prototype declaration
int getNextSequenceID(void);

int main(void) {
    printf("ID: %d\n", getNextSequenceID()); // 1
    printf("ID: %d\n", getNextSequenceID()); // 2
    printf("ID: %d\n", getNextSequenceID()); // 3
    return 0;
}

int getNextSequenceID(void) {
    // static variable retains its value between function calls!
    static int currentID = 0;
    currentID++;
    return currentID;
}
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

What happens to a local variable inside a C function when declared with the `static` keyword?