![]() | functions/eq-sec-degree-var2.c - Illustration af ulovlig anvendelse af en lokal variabel. | Lektion 5 - slide 6 : 30 Program 1 |
#include <stdio.h> #include <math.h> /* Find the roots in a * x*x + b * x + c = 0 */ void solveQuadraticEquation(void){ double a, b, c, discriminant; printf("Enter coeficients a, b, and c:\n"); scanf("%lf %lf %lf", &a, &b, &c); discriminant = b * b - 4 * a * c; if (discriminant < 0) printf("No roots\n"); else if (discriminant == 0) printf("One root: %f\n", -b/(2*a)); else printf("Two roots: %f and %f\n", (-b + sqrt(discriminant))/(2*a), (-b - sqrt(discriminant))/(2*a) ); } int main(void) { solveQuadraticEquation(); printf("Diskriminanten var: %f", discriminant ); /* WRONG: discriminant does not exist here */ /* The compiler finds this error */ return 0; }