next up previous
Next: The C Standard Up: No Title Previous: Compiler Directives

ANSI C vs Old C

 

This Appendix describes some of the differences between the ANSI C standard and ``old'' or traditional C.

  1. 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.

  2. 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.

  3. 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.

  4. Traditional C converts all float types in an expression to double precision types discussed in Chapter gif. 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.

  5. The type signed char is not available in traditional C. It is best to assume that char is unsigned.

  6. In light of the above, since returned value of getchar() might be negative, it must be assigned to an integer type.

  7. 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.

  8. It is best to think of character type in terms of integer type ASCII value in all situations.

  9. 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.

  10. Many old C compilers expect that the first character of a preprocessor directive line is a #. No leading white space is allowed.

  11. The signed type qualifier is not available in old C.

  12. The unsigned qualifier can be used to qualify integer types only (of any size).

  13. The unary positive sign is not allowed in old C.

  14. The type long double is not available in old C.

  15. The const qualifier is not available in old C.

  16. Enumeration type is not available in old C.

  17. 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.

  18. 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.



next up previous
Next: The C Standard Up: No Title Previous: Compiler Directives



Tep Dobry
Mon Dec 9 05:56:25 HST 1996