Preliminary: some historical background of C

  • The designer of the C programming language (Dennis Ritchie) wants to:

    • make coding in C fast (= less typing)...

  • Example:

      • C minimizes typing by using short hand operators

        Instead of writing:     myVariable = myVariable + 1;
        You can write:            myVariable += 1;     or      myVariable++;

  • Another area where C minimizes typing is:

      • Function declarations

  • Ritchie made the C compiler do something "strange" to avoid writing function declarations...

The implicit function declaration made by the C compiler

  • Ritchie noticed that the most commonly used data types of parameters in functions are:

      • int

  • The C compiler that Richie designed has a built-in implicit function declaration mechanism:

      • When a function is used (= called) before it was defined, the C compiler will assume that:

        • The data type of each parameter is int

        • The return data type of the function is int

  • The implicit function declaration mechanism was expanded later as:

      • The data type of the parameter for an integer typed value is int

      • The data type of the parameter for a floating point typed value is double

      • The return data type of the function is int

Example implicit function declaration

  • The following C program can be compiled and run correctly without a function declaration:

    int main( )
    {
       float a = 4;
       float b;
    
       b = square(a);  // C compiler assumed: int square(double)
    
       printf("Square of %f = %f\n", a, b);
    }
    
    int square(double x)
    {
       return x*x;
    }

    because the C compiler assumed (and it happens to be correct):

        int  square( double a )  // because a is a float variable
    

DEMO: demo/C/set1/param3.c

What happens if the implicit function declaration is wrong ?

  • The following C program cannot be compiled because the C compiler detected a type conflict:

    int main( )
    {
       float a = 4;
       float b;
    
       b = square(a);  // Assumes: int square(double)
    
       printf("Square of %f = %f\n", a, b);
    }
    
    double square(double x)
    {
       return x*x;
    }

    because the C compiler assumed (and it finds out later that it was wrong):

        int  square( double a )  
    

DEMO: demo/C/set1/param3b.c
param3b.c:13:8: error: conflicting types for `square'

Recommendation for beginning C programmers

  • Prime directive in C programming:

    • Always declare your global variables and functions

  • Good news:

      • We can use a C header file (1 header file per C program file) to declare all global things (= global variables and functions) that are defined inside the C program file

    I will discuss this C programming practice very shortly (in the topic "Multiple Files C programs")

     

     

Final comments

  • Declarations are only used with:

      1. Global variables (these variables are in the global scope)

      2. Functions (all function definitions in C are in the global scope)


  • Local variables cannot be declared


  • Important situation where you must to use declarations:

    • A C program that consists of multiple files !

    You must declare:

      • global variables defined inside another C program file
      • functions defined inside another C program file

    before you can use them.

    We will cover them soon !