Expert C Programming

(Jeff_L) #1
the function declaration. Prototypes make it easy for a compiler to check function
use with definition.


  1. The second category is new keywords. Several keywords were officially added:


enum for enumerated types (first seen in late versions of pcc), const,


volatile, signed, void, along with their associated semantics. The never-


used entry keyword that found its way into C, apparently by oversight, has been


retired.


  1. The third category is that of "quiet changes"—some feature that still compiles, but
    now has a slightly different meaning. There are many of these, but they are mostly
    not very important, and can be ignored until you push the boundaries and actually
    stumble across one of them. For example, now that the preprocessing rules are
    more tightly defined, there's a new rule that adjacent string literals are
    concatenated.

  2. The final category is everything else, including things that were argued over
    interminably while the language was being standardized, but that you will almost
    certainly never encounter in practice, for example, token-pasting or trigraphs.
    (Trigraphs are a way to use three characters to express a single character that a
    particularly inadequate computer might not have in its character set. Just as the


digraph \t represents "tab", so the trigraph ??< represents "open curly brace".)


The most important new feature was "prototypes", adopted from C++. Prototypes are an extension of
function declarations so that not just the name and return type are known, but also all the parameter
types, allowing the compiler to check for consistency between parameter use and declaration.
"Prototype" is not a very descriptive term for "a function name with all its arguments"; it would have
been more meaningful to call it a "function signature", or a "function specification" as Ada does.


Software Dogma


The Protocol of Prototypes


The purpose of prototypes is to include some information on parameter types (rather than
merely giving the function name and return value type) when we make a forward
declaration of a function. The compiler can thus check the types of arguments in a function
call against the way the parameters were defined. In K&R C, this check was deferred till
link time or, more usually, omitted entirely. Instead of


char * strcpy();


declarations in header files now look like this:


char strcpy(char dst, const char *src);


You can also omit the names of the parameters, leaving only the types:

Free download pdf