![[Dept of Engineering]](http://www.eng.cam.ac.uk/images/house_style/engban-s.gif)
Next: Maths
Up: ANSI C
Previous: ANSI C
Converting to ANSI C
Many K&R C programs compile with an ANSI C compiler without changes.
Where changes are required, the compiler will nearly always tell you.
A list of differences between K&R C and ANSI C is in [7].
The most important are
- Function prototyping :-
- Function prototypes aren't mandatory in ANSI C, but they improve
error checking. Their use enables certain ANSI C features which otherwise,
for backward compatibility, are suppressed.
- Parameter Passing :-
-
- Floats are passed as floats (in K&R C floats are converted to
doubles when passed to a function)
- Arguments are automatically cast into the right form for the
called function. Without the function prototyping the following
program wouldn't work because `mean' is expecting 2 integers.
#include <stdio.h>
#include <stdlib.h>
int mean(int a,int b)
{
return a + b;
}
main()
{
int i;
float f;
int answer;
i = 7;
f= 5.3;
/* deliberate mistake! */
answer = mean(f,j);
printf("%f + %d = %d\n", f, j, answer);
}
- Standardisation :-
- The standard include files for ANSI C are
assert.h |
Assertions |
ctype.h |
Character identification |
errno.h |
Error handling |
float.h |
Max and Min values for floats |
limits.h |
limits for integral types |
locale.h |
Internationalisation info |
math.h |
Advanced math functions |
setjmp.h |
Non-local jump |
signal.h |
Exception handling |
stdarg.h |
Variable numbers of arguments |
stddef.h |
Standard definitions |
stdio.h |
Input/Output |
stdlib.h |
General Utilities |
string.h |
String Manipulation |
time.h |
Date and Time functions |
If you want to support both ANSI C and K&R C , you can use the
following construction
#ifdef __STDC__
/* ANSI code */
#else
/* K and R code */
#endif
Next: Maths
Up: ANSI C
Previous: ANSI C
Tim Love
1999-10-06