Computer science
2009-2010
C programming language
lecture 2
Aims of lesson 2
Introduction to modularity with functions
Creation of your first own library ‘‘file.h’’ and
modularity between files
Generation of random numbers
Creation of a source file
Creation of a source file (.c):
1) Creation of the solution and of a project : fichier/nouveau -> projet
Projet Win 32 (application console win 32)
Nom : enter the name of the project (td2 for this lesson)
Create a file for the solution : You will use it (almost) all the year :
Name it with the two family names in your group
Projet vide
2) Creation of a source file: right-clic on fichiers source, ajoutez
un nouvel élément, force the extension.c
For the vzrious exercices of the lesson, name it mainfile.c
The functions
Declaration : prototype or header
Type function1(type1 a , type2 b ,…) ;
type returned by the function
Minimum needed to compile
formal argument
main()
{…
function1(x1 , x2 ,…) ;
function1(y1 , y2 ,…) ;
…
effective argument
}
Definition :
Type function1(type1 a , type2 b ,…)
{
formal argument
…
return … ;
}
1st call
2nd call
An example
Declaration : prototype or header
double poly (double u , int m , int n) ;
main( )
{
double A ;
int i , j ;
A = 5.5 ;
2 calls of the function
i=j=3;
printf(‘‘%lf %lf’’ , poly(A,i,j),poly(1,4,7)) ;
}
Definition :
double poly (double u , int m , int n)
{
double val ;
val = u*u + m*u + n ;
return val ;
}
The main function
The execution of the program begins with this function
void main(void)
{
…
}
main( )
{
…
}
int main( )
{
…
return 0 ;
}
Visibility and lifetime of the variables
Local variable
1)Variable which is declared inside a block {…}
Can only be used inside the block
Lifetime = duration of the block
2)In the header of a function (formal argument)
Global variable
Variable which is declared outside any function
Be as local as possible !
An example
int function (int a) ;
main( )
{
int i , A =1;
for (i = 0; i<10;i++)
{
int x = i*A ;
A=A+x;
printf(‘‘%d\n’’ , function(A));
}
printf(‘‘%d\n’’, function(A));
}
int function (int a)
{
int val , i ;
for (i = 0; i<20;i++) {
…}
return val ;
}
Arrays as argument of a function
Declaration :
double mean(double T[ ], int n);
Call of the function :
double x ;
double tab[N] ;
…
x = mean(tab, N);
N is a global constant (#define N 100)
Definition :
double mean(double T[ ], int n)
{ …}
General structure of a program
#include …
#define …
Declarations of the functions (prototypes)
main() {
Function calls
…}
Definition of the functions
Compulsory model for writing a function
/* dicho : search the roots of an equation by dichotomy
INPUTS :
- a,b : interval of search – suppose that f(a)*f(b)<0
- eps : precision of the solution
- f : function under study
OUTPUTS :
- roots of the equation
**** N.XXX - 27/06/08
*/
double dicho(double a, double b, double eps, double (*f) (double))
{
…
}
Modularity and files
Aims : hierarchisation of the problems, legibility, re-use ,
separated compilation
1) Put in a same source file .c the functions with the same topic
2) Create the corresponding header file .h and include it where it is needed
Workspace binome
Project td2
main.c
#include<stdio.h>
#include’’arrays.h’’
main ( )
{
Calls functions
defined in Arrays.c
}
arrays.c
Contains all the
definitions of the
functions handling
arrays and what is
needed to compile
them
arrays.h
Contains all the
prototypes of the
functions defined
in arrays.c
Structure of a program
Workspace binome
Project lesson2
main.c
#include<stdio.h>
#include’’topic1.h’’
#include’’topic2.h’’
#include’’topic3.h’’
#define SC 100
main ( )
{
Calls functions defined in
topic1.c , topic2.c , topic3.c ,
stdio.c
}
topic1.c
topic2.c
topic3.c
…
topic1.h
topic2.h
topic3.h
…
Generation of random numbers
The function rand( ) generates random numbers
between 0 and RAND_MAX (<stdlib.h>)
Call it several times …
Initialisation with srand(time(0)) (<time.h>)
(Tentative) generator of integer random numbers
between two bounds :
int i_random(int min, int max)
{
return rand( )%(max-min)+min + 1;
}
Put it in a file random.c
Histogram
Principle
probability
N values between min and max are drawn
min
max
Possible values
probability to draw the value i = nb of occurrences / N
In practice
An array with N integer random numbers between min and
max is filled
Ex : 1 5 8 1 9 4 5 8 1 2
Function histogram : fills the probability array (what is its
size?)
Modularity and global variables
extern global variable : visible by all the files where it
is declared
static global variable : visible by a single file
main.c
#include<stdio.h>
extern int A =5 ;
main ( )
{
arrays.c
int A ;
static double B = 2.5 ;
function1( )
{
random.c
int A ;
function2( )
{
}
}
}
A is visible by the functions of the 3 files
B is visible only by the functions of arrays.c
© Copyright 2026 Paperzz