Static Functions
Part of the Object Oriented Programming section of Coddy's C journey — lesson 4 of 61.
When you split code across multiple files, every function defined in a source file is visible to other files by default. But sometimes you need helper functions that should only be used internally—functions that other files shouldn't access or even know about.
The static keyword, when applied to a function, restricts its visibility to the file where it's defined. This creates a private function that cannot be called from other source files, even if someone tries to declare it.
// math_utils.c
#include "math_utils.h"
// Private helper - only visible in this file
static int square(int x) {
return x * x;
}
// Public function - can be called from main.c
int sum_of_squares(int a, int b) {
return square(a) + square(b);
}
In this example, sum_of_squares is declared in the header and can be used anywhere. However, square is marked static, so it exists only within math_utils.c. If main.c tries to call square() directly, the linker will produce an error—the function simply doesn't exist outside its file.
This is C's way of achieving encapsulation at the file level. Static functions let you hide implementation details, keeping your public interface clean while using whatever helper logic you need internally.
Challenge
EasyLet's build a distance calculator that uses a private helper function to compute the square of a number—a detail that should stay hidden from the rest of your program.
You'll create three files:
distance.h: Your header file declaring a single public functiondistance_squaredthat takes four integers (x1, y1, x2, y2) representing two points and returns an integer. Include guards using the symbolDISTANCE_H.distance.c: Your source file containing the implementation. Create astatichelper function calledsquarethat takes an integer and returns its square. This helper should only be visible withindistance.c. Then implementdistance_squaredusing your static helper to calculate (x2-x1)² + (y2-y1)².main.c: Your main file that includes the header and uses the public function. Thesquarehelper should not be accessible here—onlydistance_squaredis available.
The distance_squared function calculates the squared Euclidean distance between two points. By keeping the square function static, you're hiding this implementation detail from any code outside distance.c.
You will receive four integer inputs: x1, y1, x2, and y2.
Print the squared distance in this format:
Squared distance: {result}For example, with points (0, 0) and (3, 4), the output would be:
Squared distance: 25Cheat sheet
The static keyword restricts a function's visibility to the file where it's defined, creating a private function that cannot be called from other source files.
// Private helper - only visible in this file
static int square(int x) {
return x * x;
}
// Public function - can be called from other files
int sum_of_squares(int a, int b) {
return square(a) + square(b);
}
Static functions provide file-level encapsulation, allowing you to hide implementation details while keeping your public interface clean.
Try it yourself
#include <stdio.h>
#include "distance.h"
int main() {
int x1, y1, x2, y2;
scanf("%d", &x1);
scanf("%d", &y1);
scanf("%d", &x2);
scanf("%d", &y2);
// TODO: Call distance_squared with the input coordinates
// and print the result in the format: "Squared distance: {result}"
return 0;
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Modular Programming Basics
Header FilesInclude GuardsSource FilesStatic FunctionsRecap: Modular Calculator4Encapsulation
Opaque Pointers ConceptDefining Opaque StructsGetters and SettersValidation in SettersRecap: Secret Box2Objects and Methods
Structs as ObjectsThe 'Self' PointerConst CorrectnessPointer vs ValueHelper MethodsRecap: Point Manager5Project: Simple Bank Account
Project SetupImplementation of Account3Object Lifecycle
Constructor PatternDestructor PatternStack InitializationDeep CopyRecap: String Wrapper6Inheritance via Composition
Struct EmbeddingThe First Member RuleAccessing Parent MembersUpcastingRecap: Shape Hierarchy