#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Issue with <math.h> and sqrt() [C]


    My compiler isn't recognizing sqrt() despite including <math.h>. Help? The compiler error is "main.c:16: undefined reference to `sqrt'"

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    	int a, b, c;
    	float x1, x2;
    
    	printf("a: ");
    	scanf("%d", &a);
    	printf("b: ");
    	scanf("%d", &b);
    	printf("c: ");
    	scanf("%d", &c);
    
    	x1 = (-b + sqrt((b*b)-(4*a*c)))/(2*a);
    	x2 = (-b - sqrt((b*b)-(4*a*c)))/(2*a);
    
    	printf("x1 = %.3f", x1);
    	printf("x2 = %.3f", x2);
    
    	return 0;
    }
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,952
    Rep Power
    4033
    If your using GCC, you need to compile it with the -lm flag in order to include the math library which is where the functions from math.h are defined.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

IMN logo majestic logo threadwatch logo seochat tools logo