← Back to C Course | Chapter 5: Functions | Lesson 4 of 8

C Function Prototypes

A function prototype is an announcement of a function's name and inputs before it is written, like a table of contents entry. It lets C know the function exists.
Syntax
c
return_type function_name(type param1, type param2);  // prototype

return_type function_name(type param1, type param2) {  // definition
  // body
}

What is a Function Prototype?

A function prototype declares a function's return type, name, and parameter types to the compiler before the function's full body appears later in the file, so the compiler knows how to correctly interpret any earlier calls to it.

Example: What is a Function Prototype?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b);
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", add(2, 3));
	// Return 0 to signal that the program finished successfully
	return 0;
}
int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}

Why Use Prototypes?

Because C compilers read a file top to bottom, calling a function before its definition appears would otherwise cause an error -- prototypes solve this by giving the compiler enough information about the function upfront.

Example: Why Use Prototypes?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n);
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", square(4));
	// Return 0 to signal that the program finished successfully
	return 0;
}
int square(int n) {
	// Send n * n back to the caller
	return n * n;
}

Prototype Syntax

A prototype repeats the function's header exactly as it will appear in the definition, but ends with a semicolon instead of a body, and the parameter names inside it are optional since only their types actually matter to the compiler.

Example: Prototype Syntax

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int, int);
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", add(3, 4));
	// Return 0 to signal that the program finished successfully
	return 0;
}
int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}

Preventing Compiler Warnings

Modern compilers check every function call against its prototype's declared parameter types and count, catching mismatched arguments as a compile error rather than letting them slip through as a runtime bug.

Example: Preventing Compiler Warnings

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b);
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", add(2, 3));
	// Return 0 to signal that the program finished successfully
	return 0;
}
int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}

Multiple Prototypes

Listing all of a program's function prototypes together near the top of the file is standard C practice, giving anyone reading the file a quick overview of every function it defines before digging into the implementations.

Example: Multiple Prototypes

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b);
int square(int n);
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d %d", add(2, 3), square(4));
	// Return 0 to signal that the program finished successfully
	return 0;
}
int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}
int square(int n) {
	// Send n * n back to the caller
	return n * n;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling a function defined after main without a prototype, which gives an implicit declaration warning or error.
  2. Writing a prototype whose parameter types differ from the definition, so the compiler reports conflicting types.
  3. Forgetting the semicolon at the end of a prototype line, which causes a syntax error.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.