What are K&R Style C Functions- C Programming Course Online

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

K&R Style C Functions

Lecture 22 of our free C programming course: the old style function definition in C, why it still compiles and why you should avoid it in embedded C programming.

Welcome to the next lecture of our free C programming course. Here we study the old style function definition in C, also called the K&R style after Kernighan and Ritchie, the authors of the original C book. Every modern tutorial teaches the newer prototype style, yet if you open a vendor driver or a legacy bootloader you will still find functions written the old way.

This free C tutorial covers both halves of the old style: the old style declaration and the old style definition. You will see what they look like, why compilers still accept them, what silent bugs they allow, and why you should write every new function in the modern style. It is essential background for anyone learning embedded C programming who may inherit decades-old code.

What You Will Learn

  • The syntax of the old style function definition in C and how it differs from the modern form
  • What an old style declaration with empty parentheses does and does not tell the compiler
  • How K&R parameters are declared between the header and the opening brace
  • Why old style code lets you call a function with the wrong number of arguments
  • How to detect old style code with compiler warnings
  • What the C23 standard changed about empty parentheses and old definitions
  • How to convert old style code safely in a legacy embedded project

Prerequisites

Before you start

  • You know the three parts of a function: declaration, definition and call.
  • You understand formal and actual arguments from the earlier lectures of this chapter.
  • You can compile with gcc and read a warning message.

A Short History: K&R C and ANSI C

The first widely used description of C was the book by Kernighan and Ritchie, published in 1978. In that version of the language a function declaration could not describe its parameters at all, and a function definition listed only parameter names in the header. The types were given on separate lines afterwards. Compilers built around that description are called K&R compilers.

In 1989 the ANSI committee standardised C and introduced prototypes: declarations that carry the parameter types. Compilers based on ANSI C support both ways of writing function headers, so the large amount of existing K&R code kept compiling. That backward compatibility is the only reason the old style is still alive.

EraDeclaration carries parameter types?Definition syntax
K&R C (1978)No, only the return type and nameNames in the header, types on following lines
ANSI C (1989) and laterYes, through prototypesTypes inside the parentheses
C23Empty parentheses now mean (void)Old style definitions removed from the standard

The Old Style Function Declaration

The old style declaration names the return type and the function, followed by a pair of empty parentheses:

return_type func_name();

The parentheses are always empty, even when the function really does receive arguments. The compiler therefore learns nothing about how many arguments there are or what their types are. Modern compilers still accept this convention so that old programs keep working, but it is no longer used in new code because the prototype form gives the compiler far more information.

Old style versus prototype declaration

Old style: int ep_add();

Tells the compiler only that ep_add exists and returns int. Argument count and types are unknown, so no checking happens.

Prototype: int ep_add(int a, int b);

Tells the compiler the exact number and type of arguments. Wrong calls are reported at compile time.

What goes wrong with an old style declaration

Because the compiler cannot check the arguments, a wrong call compiles quietly. Study this program. The declaration is old style, but the definition has two parameters, and the call passes only one:

Program oldproto.c

#include <stdio.h>

int ep_add();

int main(void)
{
    printf("%d\n", ep_add(5));
    return 0;
}

int ep_add(int a, int b)
{
    return a + b;
}

Compiled with gcc -std=c11 -Wall -Wextra there was no warning at all, and the program printed an unpredictable garbage number (we saw -1064857443 on one run, you will see something else). The second argument b was never supplied, so the function added 5 to whatever happened to be in that register or stack slot. That is undefined behaviour, and it is exactly the class of bug that prototypes were invented to catch.

Now we ask the compiler to be stricter. Adding -Wstrict-prototypes produces this warning:

Compiler warning

oldproto.c:3:1: warning: function declaration isn't a prototype [-Wstrict-prototypes]
    3 | int ep_add();
      | ^~~

And in C23 mode (-std=c2x on GCC 13) the empty parentheses mean (void), so the call becomes a hard error:

oldproto.c:7:20: error: too many arguments to function 'ep_add'
    7 |     printf("%d\n", ep_add(5));
      |                    ^~~~~~
oldproto.c:3:5: note: declared here
    3 | int ep_add();
      | ^~~~~~
Watch out: In C11 and earlier, int f(); means “unknown arguments”. In C23 it means int f(void);. The same source can compile in one mode and fail in another.

The Old Style Function Definition

The old style function definition looks slightly different from the one you have studied. Instead of putting the types inside the parentheses, you list only the parameter names, declare each one on the lines that follow, and only then open the body:

return_type func_name(argument list)
argument declarations
{
    local variable declarations;
    statements;
    return (expression);
}

Here is a complete, original K&R style function that mixes two integers and scales the sum by a floating-point gain, in the spirit of a small audio or sensor routine:

Program kr_style.c

#include <stdio.h>

int ep_mix(a, b, gain)
int a, b;
float gain;
{
    return (int)((a + b) * gain);
}

int main(void)
{
    printf("mix = %d\n", ep_mix(10, 20, 1.5f));
    return 0;
}

Output:

mix = 45

Read the header slowly. The parentheses contain only the names a, b and gain. The next two lines give their types. Everything after the opening brace is ordinary C. Compiling with -Wold-style-definition makes GCC point at the function:

kr_style.c:3:5: warning: old-style function definition [-Wold-style-definition]
    3 | int ep_mix(a, b, gain)
      |     ^~~~~~
Anatomy of a K&R definition
1

Header

Return type, function name and a bare list of parameter names: int ep_mix(a, b, gain).

2

Parameter declarations

One or more lines that give each name a type: int a, b; and float gain;.

3

Opening brace

The body starts only after every parameter has been declared.

4

Body

Local variables, statements and the return statement as usual.

In the example, ep_mix was called with 1.5f. In K&R style a call without a prototype causes every float argument to be promoted to double, and the callee converts it back to the declared type. This is called the default argument promotion. It works here, but it is one more rule that prototypes make unnecessary.

Compiler Support for the Old Style Function Definition in C

Compilers based on K&R C support only the old syntax, while newer compilers based on ANSI C support both ways of writing function headers. We compiled the K&R example with -std=c11 and it worked, with only the optional warning. We also compiled it with -std=c2x on GCC 13.3 and it was still accepted with the same warning. Be careful with that result: the C23 standard itself removed old style definitions, so a compiler that fully implements C23 is allowed to reject them, and future versions probably will.

Compiler modeOld declaration int f();Old definition (K&R)
-std=c11Accepted, arguments uncheckedAccepted, warning with -Wold-style-definition
-std=c2x on GCC 13.3Means f(void), wrong calls are errorsStill accepted with a warning in our test
Fully conforming C23 compilerMeans f(void)May be rejected

Spotting Mistakes in Old Style Code

Old style code often hides small typos that a prototype would expose. Consider this routine, written in K&R style to compute an average of two integers divided by a count:

Program bookbug.c

#include <stdio.h>

float ep_avg_old(a, b, z)
int a, b;
float z;
{
    float s;
    s = (a + b) / z;
    return z;
}

int main(void)
{
    printf("avg expected 15.0, got %.1f\n", ep_avg_old(10, 20, 2.0f));
    return 0;
}

Output:

avg expected 15.0, got 2.0

The function calculates s but returns z, the divisor, so the result is simply 2.0. GCC only warns variable 's' set but not used when -Wall -Wextra is on. A traditional textbook example has this very slip, which is a good reminder to read the return line of every function you copy. Always switch warnings on, and never trust a function that compiled silently.

Why You Should Not Write the Old Style Today

AspectOld stylePrototype style
Argument count checkingNoneCompiler checks every call
Argument type checkingNone, only default promotionsCompiler converts or reports errors
ReadabilityNames and types separatedEverything in one line
Tool supportLinters warn about itStandard and recommended
FutureRemoved in C23Fully supported

Safety standards for embedded software, including the MISRA C guidelines, expect declarations to carry full parameter information. If you work on automotive or industrial firmware, old style functions will be flagged during review.

Converting an Old Style Function Definition in C Safely

When you inherit a legacy file, convert it one function at a time. The recipe is short:

  1. Compile once with -Wall -Wextra -Wold-style-definition -Wstrict-prototypes and save the warning list.
  2. For each function, move the parameter types into the parentheses and delete the separate declaration lines.
  3. Replace every empty-parenthesis declaration with a real prototype, using (void) for functions that truly take no arguments.
  4. Fix every new warning, because each one is a call site that was silently wrong before.
  5. Run your tests, then compile again in a newer language mode to confirm the code is ready for C23.

Common Mistakes and Troubleshooting

MistakeEffectFix
Writing int f(); and assuming it means no argumentsUnknown arguments in C11, no arguments in C23Write int f(void);
Mixing an old declaration with a prototype style definitionCompiles, but calls are uncheckedUse a matching prototype everywhere
Forgetting a parameter declaration lineThe parameter silently defaults to int in old compilersDeclare every parameter explicitly
Ignoring warnings in legacy codeHidden undefined behaviourEnable the strict warning flags

Best Practices and Security Considerations

Best practices

  • Write every new function with a full prototype, and use (void) for empty parameter lists.
  • Keep prototypes in header files so every caller sees the same declaration.
  • Turn on -Wstrict-prototypes and -Wold-style-definition in your build.
  • Convert legacy functions gradually and test after each change.

If a code review flags an old style function definition in C, treat it as a to-do item rather than an emergency: the code may be correct today, but it gives you no safety net when someone changes a caller tomorrow.

From a security point of view an unchecked argument list is dangerous. A missing argument means the function reads an indeterminate value from a register or the stack. In firmware that handles external input, such behaviour can be exploited or can simply crash a device in the field.

Summary and Key Takeaways

Remember

  • The old style declaration has empty parentheses and gives the compiler no information about arguments.
  • The old style function definition lists names in the header and declares their types on following lines.
  • Compilers still accept both for compatibility, but C23 changed the meaning of empty parentheses and removed old definitions.
  • Wrong calls to old style functions compile silently and cause undefined behaviour.
  • Always write modern prototypes in new embedded C code.

Conclusion

You can now read an old style function definition in C without confusion, understand why it still compiles, and see why teams are retiring it. In this free embedded C course we keep the old forms in view only so that legacy code never surprises you. In the next lectures we use the clean prototype style to build real programs: reversing digits, testing primes and converting numbers between bases.

Frequently Asked Questions

What is an old style function definition in C?

It is the K&R syntax where the parentheses hold only parameter names and the types are declared on the lines before the opening brace.

Does int f(); mean the function takes no arguments?

In C11 and earlier it means the arguments are unspecified. In C23 it means the same as int f(void);.

Are old style functions still supported by compilers?

Most compilers still accept them for compatibility, usually with an optional warning. The C23 standard removed old style definitions, so support may disappear.

Why are old style functions dangerous?

The compiler cannot check argument count or types, so a wrong call compiles silently and causes undefined behaviour.

Which GCC flags reveal old style code?

Use -Wold-style-definition for definitions and -Wstrict-prototypes for declarations.

Do I need to learn K&R style for embedded jobs?

You need to read it, because legacy drivers and bootloaders use it, but you should always write the prototype style yourself.

Is this free C tutorial suitable for beginners?

Yes. This free C programming course starts from basics and explains legacy syntax only as background for embedded C programming.

Keep Going With the Free Embedded C Course

Next we use clean prototype-style functions to build a real program that reverses the digits of a number.

<

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *