K&R C Syntax: Old-Style Function Declarations Explained

K&R syntax is the pre-ANSI C form for declaring a function’s parameters. The names appear in the function header and their types are declared between the header and opening brace. It is historical C, not a style for modern code.

Old-style versus modern declarations

/* K&R / old-style definition */
int add(left, right)
int left;
int right;
{
    return left + right;
}

/* Modern C definition */
int add(int left, int right) {
    return left + right;
}

Why it matters historically

Early C did not have prototypes in the modern sense. A declaration such as int add(); meant the function returned int but did not specify parameter types. That weak checking made mismatched calls easier to write. ANSI C introduced prototypes, and modern C practice is to declare every parameter type explicitly.

Do not rely on implicit int

Older dialects permitted omitted types to default to int. Modern C removed implicit int; a conforming modern compiler should diagnose it. C++ does not support K&R function definitions.

When you might see it

You may encounter old-style definitions in legacy Unix code, very old books, or archaeology of pre-standard projects. Preserve them only when maintaining code under a constrained legacy toolchain. When modernizing, convert declarations and add tests before changing behavior.

Reference: C function declarations.

Comments

Leave a Reply

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