#include #include /* * static.c -- demonstrates the use static variables in a function */ // forward declaration double dist ( double x, double y ); int main ( int argc, char *argv[] ) { /* Call dist() with new coordinates to get new drift distance * Old coordinates will be managed by dist() function through static variables. */ printf("drift: %g\n", dist(1.0, 1.0)); printf("drift: %g\n", dist(1.0, 2.0)); printf("drift: %g\n", dist(2.0, 2.0)); printf("drift: %g\n", dist(0.0, 0.0)); return 0; } double dist ( double x, double y ) { static double ox = 0.0; static double oy = 0.0; double d, dx, dy; dx = x - ox; dy = y - oy; d = sqrt ( dx * dx + dy * dy ); ox = x; oy = y; return d; }