This Appendix describes some of the differences between the ANSI C standard
and ``old'' or traditional C.
-
In traditional C, function declarations are assumed by default to
be of type int.
In other words, integer type functions need not declared at all.
Here is an example of a program in old C with a function definition.
#include <stdio.h>
main()
{ int x;
x = 3;
printf("Square of %d = %d\n", x, square(x));
}
square(x)
int x;
{ int y;
y = x * x;
return y;
}
The function main() uses
square() without a declaration for it. Also, when square() is
defined, a return type is not specified; int is assumed.
-
In old C,
function headers have different syntax.
As shown in the above program, a function is defined with a
parenthesized list of formal parameter variable names in
the header without their types. The formal
parameters are then declared outside the function body. Any automatic variables
needed in a function are then declared in the function body in a normal manner.
-
Function prototypes must be declared without a list of arguments and types,
and consist of the type, function name and an
empty set of parentheses, for example:
float fabs();
Parentheses must be included to indicate function declarations.
Usually, only non-integer function types are declared.
As a result, old C compilers do not to type checking on arguments
passed in function calls.
-
Traditional C converts all float types in an expression to double precision
types discussed in Chapter
.
This requires that all floating point formal
parameters in function definitions must be type double, not float.
For example:
#include <stdio.h>
main()
{ float x, y;
float fsquare();
x = 3.0;
printf("Square of %f = %f\n", x, fsquare(x));
}
float fsquare(x)
double x;
{ int y;
y = x * x;
return y;
}
When main() calls fsquare()
the argument expression is x; the float type of x
is converted to type double and passed to fsquare().
If the formal parameter in
fsquare() is declared to be a float object,
then it will attempt to interpret a
double type object as a float type resulting in a serious error.
Therefore, in old C,
declare all floating point formal parameters as double.
-
The type signed char is not available in traditional C.
It is best to assume that char is unsigned.
-
In light of the above,
since returned value of getchar() might be negative,
it must be assigned to an integer type.
-
Characters are always converted to integers in expressions. Formal
parameters in such function definitions must be int
type for the same reason that
floating point parameters should be double.
-
It is best to think of character type in terms of integer type ASCII value
in all situations.
-
Type void is not available in old C.
Functions are assumed to return integer if
they return nothing. Since default declaration is used extensively in old C,
functions that return nothing are considered, by default, to return integer.
-
Many old C compilers expect that the first character of a preprocessor
directive line is a #. No leading white space is allowed.
-
The signed type qualifier is not available in old C.
-
The unsigned qualifier can be used to qualify integer types only
(of any size).
-
The unary positive sign is not allowed in old C.
-
The type long double is not available in old C.
-
The const qualifier is not available in old C.
-
Enumeration type is not available in old C.
-
Automatic arrays cannot be initialized in declarations in old C.
Only external and static arrays can be so initialized.
Therefore, automatic static arrays are often used primarily so that they can be
initialized.
-
Automatic structures and arrays of structures cannot be initialized in
declarations in old C. Some old compilers may not allow references to entire
structures, requiring the use of structure pointers or individual structure
members.