Δημοσιεύτηκε: 25 Ιαν 2012, 04:47
Δες κι άλλο παράδειγμα...
Έξοδος:
ΥΓ. Σε real-life προγράμματα στο .h πρέπει να βάλεις include-guards.
- Κώδικας: Επιλογή όλων
/* FILE: libcalc.c */
/*********************************************************//**
*
************************************************************/
int add( const int x, const int y )
{
return x+y;
}
/*********************************************************//**
*
************************************************************/
int sub( const int x, const int y )
{
return x-y;
}
/*********************************************************//**
*
************************************************************/
int mul( const int x, const int y )
{
return x * y;
}
/*********************************************************//**
*
************************************************************/
int dvs( const int x, const int y )
{
return y == 0 ? 0 : x/y;
}
- Κώδικας: Επιλογή όλων
/* FILE: libcalc.h */
extern int add( const int x, const int y );
extern int sub( const int x, const int y );
extern int mul( const int x, const int y );
extern int dvs( const int x, const int y );
- Κώδικας: Επιλογή όλων
/* FILE: calctest.c */
#include <stdio.h>
#include "libcalc.h"
/*********************************************************//**
*
************************************************************/
int main( void )
{
int x = 10, y = 2;
printf( "x+y = %d, x-y = %d, x * y = %d, x / y = %d\n",
add(x,y), sub(x,y), mul(x,y), dvs(x,y)
);
return 0;
}
- Κώδικας: Επιλογή όλων
gcc -c libcalc.c
- Κώδικας: Επιλογή όλων
gcc calctest.c libcalc.o -o calctest
- Κώδικας: Επιλογή όλων
./calctest
Έξοδος:
- Κώδικας: Επιλογή όλων
x+y = 12, x-y = 8, x * y = 20, x / y = 5
ΥΓ. Σε real-life προγράμματα στο .h πρέπει να βάλεις include-guards.