Expert C Programming

(Jeff_L) #1
precision meant that the PDP-11 could just be set in "double" mode and left to crank away,
without keeping track of the precision.


  • You can do a lot of C programming without ever becoming aware of the default type
    promotions. And many C programmers do.

  • You can't call yourself an expert C programmer unless you know this stuff. It gains particular
    importance in the context of prototypes, described in the next section.


Prototype Painfulness


The purpose of ANSI C function prototypes is to make C a more reliable language. Prototypes are
intended to reduce a common (and hard-to-find) class of errors, namely a mismatch between formal
and actual parameter types.


This is accomplished by a new form of function declaration that includes the parameter declarations.
The function definition is also changed in a similar way, to match the declaration. The compiler can
thus check use against declaration. As a reminder, the old and new forms of declaration and definition
are shown in Table 8-2.


Table 8-2. K&R C Function Declarations Compared with ANSI C Prototypes
K&R C ANSI C
Declaration:

int foo();


Prototype:

int foo(int a, int b);


or

int foo(int, int );


Definition:

int foo(a,b)


int a;


int b;


{


...


}


Definition:

int foo(int a, int b)


{


...


}


Notice that a K&R function declaration differs from an ANSI C function declaration (prototype), and
a K&R function definition differs from an ANSI C function definition. You express "no parameters"
in ANSI C as int foo(void); so even this case looks different from classic C.


However, ANSI C didn't and couldn't insist on the use of prototypes exclusively, because that would
have destroyed upward compatibility for billions of lines of existing pre-ANSI code. The standard
does stipulate that the use of function declarators with empty parentheses (i.e., without specifying
argument types) is officially declared obsolescent, and support for it may be withdrawn from future

Free download pdf