Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 135 ยท Functions

Declarations, Prototypes and Headers

Learn how declaring and defining a C function differ, why prototypes matter, what (void) versus () means before and after C23, and how header files work.

The C compiler reads your file from top to bottom, once. When it meets a call like area(3, 4), it needs to know right then what area is: how many parameters it takes, of which types, and what it returns. So far you've satisfied it by defining every function above the code that calls it. That works for small programs, but it forces an awkward order (main at the very bottom), it fails for functions that call each other, and it can't work across files at all.

The solution is a declaration, also called a prototype: a one-line promise that says "a function with this signature exists; its body is somewhere else". This lesson explains declarations versus definitions, what goes wrong without prototypes, the confusing history of empty parentheses, and how header files like <stdio.h> are simply collections of prototypes.

Declaration versus definition

A definition creates the function: header plus body. A declaration only describes it: the header followed by a semicolon.

C
double area(double width, double height);     /* declaration (prototype) */

double area(double width, double height) {    /* definition */
    return width * height;
}

A function can be declared as many times as you like, as long as every declaration agrees, but it must be defined exactly once in the whole program. The definition also counts as a declaration for the code that follows it.

With prototypes at the top, you can put the functions in whatever order reads best. Many C programmers like main first, so a reader sees the big picture before the details:

C main first, thanks to prototypes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

print_table calls celsius_to_fahrenheit with an int. Because the prototype says the parameter is double, the compiler converts each c to double before the call, which is exactly what you want. That automatic conversion is one of the most important jobs of a prototype.

Parameter names are optional in prototypes

In a declaration, only the types matter. double area(double, double); is a valid prototype. Still, names document the meaning: void draw_rect(int, int, int, int); tells a reader nothing, while void draw_rect(int x, int y, int width, int height); explains itself. Names in a prototype don't have to match the definition's, but keeping them identical avoids confusion.

Functions that call each other

Sometimes two functions call each other. This is called mutual recursion, and with it, no ordering of the definitions works: whichever comes first calls the other before it's known. A prototype breaks the cycle:

C Mutual recursion needs a prototype

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

n % 2 == 0 would of course be the sane way to test for evenness; this pair is just the smallest possible example of two functions that depend on each other. Real cases are common in parsers, where parse_expression calls parse_term, which can call parse_expression again for a parenthesized sub-expression. You'll learn how recursion works in Recursion.

What happens without a declaration

Calling a function that the compiler has never seen is an error in modern C. Old C (C89) allowed it and silently assumed the function returned int and took whatever arguments you passed. That "implicit declaration" rule was removed in C99, and GCC 14 and later reject such calls by default:

C
int main(void) {
    double r = half(9.0);    /* error: implicit declaration of function 'half' */
    return 0;
}

double half(double x) { return x / 2; }

That's a good thing. Under the old rule, the compiler would assume half returned an int, and the program would read the result from the wrong place and print nonsense. Many old tutorials and textbooks are full of code that relied on implicit declarations; if you see the warning or error, add the prototype or the right #include.

Mismatched declarations

If a declaration and the definition disagree, the compiler stops with conflicting types:

C
int scale(int value);                  /* says int */
double scale(double value) { ... }     /* error: conflicting types for 'scale' */

The compiler can only catch this when both are visible in the same file. When the prototype lives in one file and the definition in another, nothing checks them against each other unless the definition's file includes the same header, which is exactly why every .c file should include its own header (more below).

Empty parentheses: (void) versus ()

This is the most confusing corner of C function declarations, because its meaning changed in C23.

  • int menu(void); means "takes no arguments" in every C standard.
  • int menu(); meant "takes an unspecified number of arguments" in C89 through C17. It's a declaration without a prototype, so the compiler couldn't check calls: menu(1, 2, 3) compiled without complaint.
  • In C23, int menu(); means the same as int menu(void);, just like in C++.

Our editor compiles in C23 mode, so it rejects wrong calls even with (). This example deliberately fails to compile:

C Wrong argument count (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

GCC reports too many arguments to function 'pick_number'; expected 0, have 2. Compiled as C17 or earlier (-std=c17), the same code builds without an error, because the () declaration tells the compiler nothing to check against; the call is undefined behavior, which on most systems just ignores the extra arguments. Since code often has to compile under older standards too, always write (void) for functions without parameters. It's unambiguous everywhere.

C23 also removed old-style (K&R) function definitions, where parameter types were listed between the ) and the {, like int add(a, b) int a; int b; { return a + b; }. You may still meet them in very old code. GCC 15 still accepts them in C23 mode but warns old-style function definition, and other compilers may refuse them; rewrite them with types in the parameter list.

Header files

A header file (.h) is a file of declarations that other files pull in with #include. The preprocessor literally pastes the header's text in place of the #include line before compiling. When you write #include <stdio.h>, you get the prototypes for printf, puts, fgets and the rest. The actual code of those functions lives in the compiled C library, which the linker connects to your program (see How C Works).

You can prove that a header just provides declarations by writing one yourself instead of including it:

C A prototype instead of a header

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

It compiles and runs, because the declaration is all the compiler needs; the linker supplies the definition. Don't do this in real programs, though: the standard headers declare functions correctly for your system (some are macros, some have attributes), and they also provide types and constants like FILE and EOF.

Your own header: a preview

Once a program grows, you split it into several .c files, and each one shares its functions through a header. A typical pair looks like this:

C
/* temperature.h: the interface, what other files may use */
#ifndef TEMPERATURE_H
#define TEMPERATURE_H

double celsius_to_fahrenheit(double celsius);
double fahrenheit_to_celsius(double fahrenheit);

#endif
C
/* temperature.c: the implementation */
#include "temperature.h"    /* include your own header: the compiler checks
                               that definitions match the declarations */

double celsius_to_fahrenheit(double celsius) {
    return celsius * 9.0 / 5.0 + 32.0;
}

double fahrenheit_to_celsius(double fahrenheit) {
    return (fahrenheit - 32.0) * 5.0 / 9.0;
}
C
/* main.c: a user of the module */
#include <stdio.h>
#include "temperature.h"

int main(void) {
    printf("%.1f\n", fahrenheit_to_celsius(98.6));
    return 0;
}
Terminal
gcc -std=c17 -Wall -Wextra main.c temperature.c -o convert

Some rules of thumb you can already follow:

  • Use #include <...> for system and library headers and #include "..." for your own; quotes make the compiler search your project folder first.
  • The #ifndef / #define / #endif lines are an include guard: they make it safe to include the header twice. Conditional Compilation explains them.
  • Headers contain declarations, not function definitions; a definition in a header included by two .c files would be defined twice and the linker would complain.
  • A function used only inside one .c file should be marked static and kept out of the header. Scope and Storage Classes covers static, and Multi-File Programs and Linkage builds a full multi-file project.

Our online editor compiles a single file, so the multi-file layout above is shown read-only. You can simulate it by putting the "header" part at the top of one file, which is exactly what the preprocessor does anyway:

C A module in one file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The conversions round-trip cleanly; note that -40 is the same in both scales.

Common mistakes

  • Forgetting the semicolon after a prototype. The compiler then thinks a function body should follow and reports confusing errors on the next line.
  • A semicolon after a definition's header, like int add(int a, int b); { ... }. That's a prototype followed by a stray block, and GCC reports expected identifier or '(' before '{' token.
  • Prototype and definition that disagree. Include your own header in the .c file so the compiler compares them.
  • Writing () for "no parameters" in code that must also compile as C17 or older. Use (void).
  • Defining functions in headers that are included by several files. Put only declarations there (the exception, static inline functions, comes in inline Functions).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which line is a declaration (prototype) and not a definition?

    A header followed by a semicolon declares the function. The first option has a body, so it's a definition; the others are calls.
  2. 2. Given the prototype double half(double x);, what value does x receive in the call half(7)?

    A prototype tells the compiler the parameter types, so arguments are converted as if by assignment. The int 7 becomes 7.0 and half returns 3.5.
  3. 3. How many times may a function be defined in a whole program?

    Declarations can repeat as long as they agree, but there must be exactly one definition. Two definitions cause a "multiple definition" linker error.
  4. 4. In C17, what does int roll(); declare?

    Before C23, empty parentheses in a declaration meant "unspecified parameters". Only (void) means "no parameters" in every standard; C23 made () mean the same as (void).
  5. 5. Why should temperature.c include its own header temperature.h?

    Including the header puts the declarations and the definitions in the same file, so any mismatch becomes a "conflicting types" error instead of a silent bug.

Summary

  • A definition has a body and must exist exactly once; a declaration (prototype) is the header plus ; and may appear many times.
  • Prototypes let you call functions before their definitions, enable mutual recursion and make the compiler check and convert arguments.
  • Calling an undeclared function is an error in modern C (implicit declarations were removed in C99).
  • Write (void) for "no parameters". Before C23, () meant "unspecified parameters" and turned off argument checking; in C23 it means (void).
  • Headers are files of declarations pasted in by #include; their functions' code is linked in later. Use include guards, put only declarations in headers, and include your own header in its .c file.

Next: Recursion, where functions call themselves to solve problems one smaller step at a time.