#include <stdio.h>
int square( int x )
{
int r; // Define a local variable
r = x * x; // Statement
return ( r ); // Return statement
}
int main( int argc, char *argv[] )
{
int a, b;
a = 4;
b = square( a ); // Call function square
printf( "Square of %d = %d\n", a, b);
}
|
Notice that:
|
|
In general, to compile a C program that consists of the files file1.c, file2.c, file3.c, ...., use this command:
gcc [-o outputName] file1.c file2.c file3.c ....
Requirement:
One of the files file1.c, file2.c, file3.c ...
must contain a main() function
|
| File 1 (func1a.c) | File 2 (func1b.c) |
|---|---|
#include <stdio.h>
int square( int x )
{
int r;
r = x * x;
return ( r );
}
|
#include <stdio.h>
int main( int argc, char *argv[] )
{
int a, b;
a = 4;
b = square( a );
printf( "Square of %d = %d\n", a, b);
}
|
We can compile this C-program using the following command:
gcc func1a.c func1b.c (output is a.out)
or:
gcc -o myProg func1a.c func1b.c (output is myProg)
|
How to run the program:
|
Output:
Square of 4 = 16 |
|
double square( double x )
{
double r; // Define a local variable
r = x * x; // Statement
return ( r ); // Return statement
}
int main( int argc, char *argv[] )
{
double a, b;
a = 4;
b = square( a ); // Call function square
printf( "Square of %lf = %lf\n", a, b);
}
|
This program (stored in one file) will compile and run correctly:
Square of 4.000000 = 16.000000 |
How to run the program:
|
| File 1 (func2a.c) | File 2 (func2b.c) |
|---|---|
#include <stdio.h>
double square ( double x )
{
double r; // Local variable
r = x * x; // Statement
return ( r ); // Return
}
|
#include <stdio.h>
int main( int argc, char *argv[] )
{
double a, b;
a = 4.0;
b = square( a ); // Call square
printf( "Square of %lf = %lf\n", a, b);
}
|
Result:
Square of 4.000000 = 0.000000 |
How to run the program:
|
|
|
by:
|
(I will show you a working example in the next webpage)