Topic : Introduction to C Programming
Author : Mkoubaa
Page : << Previous 4  Next >>
Go to page :


difference in the way it executes; it is just a different notation. You should use the "new style," (known as "ANSI C") with the type declared as part of the parameter list, unless you know you will be shipping the code to someone who has access only to an "old style" (non-ANSI) compiler.
It is now considered good form to use function prototypes for all functions in your program. A prototype declares the function name, its parameters, and its return type to the rest of the program in a manner similar to a forward declaration in Pascal. To understand why function prototypes are useful, enter the following code and run it:

#include <stdio.h>

void main()  
{    
    printf("%d\n",add(3));  
}

int add(int i, int j)  
{    
    return i+j;  
}


This code compiles without giving you a warning, even though add expects two parameters but receives only one, because C does not check for parameter matching either in type or count. You can waste an enormous amount of time debugging code in which you are simply passing one too many or too few parameters. The above code compiles properly, but it produces the wrong answer.
To solve this problem, C lets you place function prototypes at the beginning of (actually, anywhere in) a program. If you do so, C checks the types and counts of all parameter lists. Try compiling the following:

#include <stdio.h>

int add (int,int); /* function prototype for add */

void main()  
{    
    printf("%d\n",add(3));  
}

int add(int i, int j)  
{    
    return i+j;  
}


The prototype causes the compiler to flag an error on the printf statement.
Place one prototype for each function at the beginning of your program. They can save you a great deal of debugging time, and they also solve the problem you get when you compile with functions that you use before they are declared. For example, the following code will not compile:

#include <stdio.h>

void main()  
{    
    printf("%d\n",add(3));  
}

float add(int i, int j)  
{    
    return i+j;  
}


Why, you might ask, will it compile when add returns an int but not when it returns a float? Because C defaults to an int return value. Using a prototype will solve this problem. "Old style" (non-ANSI) compilers allow prototypes, but the parameter list for the prototype must be empty. Old style compilers do no error checking on parameter lists.

Introduction to C Programming
Part 7: C Libraries and Makefiles


Introduction to Libraries
Libraries are very important in C because the C language supports only the most basic features that it needs. C does not even contain I/O functions to read from the keyboard and write to the screen. Anything that extends beyond the basic language must be written by a programmer. The resulting chunks of code are placed in libraries. We have seen the standard I/O, or stdio, library already: Libraries exist for math functions, string handling, time manipulation, and so on. Libraries also give you the ability to split up your programs into modules, which makes them easier to understand, test, and debug, and also makes it possible to reuse code from other programs that you write.
You can create your own libraries easily. As an example, we will take some code from tutorial 4 and make a library out of two of its functions:

#include <stdio.h>  

#define MAX 10

int a[MAX];
int rand_seed=10;

int rand()  
/* from K&R - produces a random number between 0 and 32767.*/  
{    
    rand_seed = rand_seed * 1103515245 +12345;    
    return (unsigned int)(rand_seed / 65536) % 32768;  
}

void main()  
{    
    int i,t,x,y;
  
    /* fill array */    
    for (i=0; i < MAX; i++)    
    {      
        a[i]=rand();      
        printf("%d\n",a[i]);    
    }
  
    /* bubble sort the array */    
    for (x=0; x < MAX-1; x++)      
        for (y=0; y < MAX-x-1; y++)        
            if (a[y] > a[y+1])        
            {  
                t=a[y];  
                a[y]=a[y+1];  
                a[y+1]=t;        
            }

    /* print sorted array */    
    printf("--------------------\n");    
    for (i=0; i < MAX; i++)    
        printf("%d\n",a[i]);
}


This code fills an array with random numbers, sorts them using a bubble sort, and then displays the sorted list.
Take the bubble sort code, and use what you learned in tutorial 6 to make a function from it. Since both the array a and the constant MAX are known globally, the function you create needs no parameters, nor does it need to return a result. However, you should use local variables for x, y, and t.
Once you have tested the function to make sure it is working, pass in the number of elements as a parameter rather than using MAX. Do this first without looking at the code below and then compare the two only when you have finished.

#include <stdio.h>

#define MAX 10

int a[MAX];
int rand_seed=10;

int rand() /* from K&R - returns random number between 0 and 32767.*/  
{    
    rand_seed = rand_seed * 1103515245 +12345;    
    return (unsigned int)(rand_seed / 65536) % 32768;  
}
  
void bubble_sort(int m)  
{    
    int x,y,t;
     for (x=0; x < m-1; x++)      
        for (y=0; y < m-x-1; y++)        
            if (a[y] > a[y+1])        
            {  
                t=a[y];  
                a[y]=a[y+1];  
                a[y+1]=t;        
            }
}
  
void main()  
{    
    int i,t,x,y;
    /* fill array */    
    for (i=0; i < MAX; i++)    
    {      
        a[i]=rand();      
        printf("%d\n",a[i]);    
    }
    bubble_sort(MAX);
    /* print sorted array */    
    printf("--------------------\n");    
    for (i=0; i < MAX; i++)      
        printf("%d\n",a[i]);
}


You can also generalize the bubble_sort function even more by passing in a and the size of a as parameters:

bubble_sort(int m, int a[])

This line says, "Accept the integer array a of any size as a parameter." Nothing in the body of the bubble_sortfunction needs to change. To call bubble_sort change the call to:

bubble_sort(MAX,a);

Note that &a has not been used even though the sort will change a. The reason for this will become clear in tutorial

Page : << Previous 4  Next >>