VSCode is throwing 'identifier "M_PI" is undefined' [duplicate] - c

This question already has answers here:
M_PI not available with gcc --std=c11 but with --std=gnu11?
(3 answers)
Closed 1 year ago.
I have the following code:
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>
char r[15];
double radius;
double SphereVolume(double radius){
double volume;
volume = (4.0 / 3.0) * (M_PI) * (pow(radius, 3.0)); ///'identifier "M_PI" is undefined'
return (volume);
}
int main()
{
/* gets input from the user */
printf("What is the radius of the Sphere? ");
fgets(r, sizeof(r), stdin);
sscanf(r, "%lf", &radius);
printf("Volume of the sphere is %f", SphereVolume(radius));
return (0);
}
I can run it well with gcc by doing:
$> gcc -Wall -Werror -Wextra -o SphereVolume SphereVolume.c -lm
But VS Code isn't letting me even debugging the code. What is happening? do I have messed up settings?

I think I found the answer:
M_PI not available with gcc --std=c11 but with --std=gnu11?
Apparently M_PI is not defined in standard C

Related

Gcc doesn't recognise 'double complex' decleration

I need a complex library for some stuf in c++ or c.
So I found some useful tooltip in linux.
man complex
documentation have good example like this:
#include <math.h> /* for atan */
#include <stdio.h>
#include <complex.h>
int
main(void)
{
double pi = 4 * atan(1.0);
double complex z = cexp(I * pi);
printf("%f + %f * i\n", creal(z), cimag(z));
}
everything goes well...
But I took error every time which I tried.
> Executing task: /usr/bin/g++ -g '/home/max/Documents/c_expls/test2.cpp' -o '/home/max/Documents/c_expls/test2' <
/home/max/Documents/c_expls/test2.cpp: In function ‘int main()’:
/home/max/Documents/c_expls/test2.cpp:10:17: error: expected initializer before ‘z’
10 | double complex z = cexp(I * pi);
| ^
/home/max/Documents/c_expls/test2.cpp:11:32: error: ‘z’ was not declared in this scope
11 | printf("%f + %f * i\n", creal(z), cimag(z));
| ^
The terminal process "/bin/bash '-c', '/usr/bin/g++ -g '/home/max/Documents/c_expls/test2.cpp' -o '/home/max/Documents/c_expls/test2''" terminated with exit code: 1.
Terminal will be reused by tasks, press any key to close it.
I edited code a little bit, like adding double complex z etc.. but same machine... same error...
I think my gcc installation have lacks component. beause I tried Code::Blocks
Do you have an idea,
why my gcc doesn't know this declaration?
You need to compile with gcc and specify that you want to use the C99 standard or higher -std=c99.
For C++, use std::complex.
You need to link against the math library with the flag -lm
Using exactly your code in a file called test.c I compiled with:
gcc -o test test.c -lm
Running the binary gives the output:
./test
-1.000000 + 0.000000 * i

C round function is throwing error: "undefined reference to `round'..." [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
I hope someone can help me. I am working through CS50x and am working on Pset1 - greedy. I am getting the following error whenever I compile my code:
/tmp/greedy-46be96.o: In function `main':
greedy.c:(.text+0x95): undefined reference to `round'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any help will be greatly appreciated. I apologise if the question is vague, I have tried to be in depth. I have used man round in the terminal, and have searched everywhere, trying different solutions, but nothing has worked.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
float owed;
float change;
float quarter = 0.25;
float dime = 0.10;
float nickel = 0.05;
float penny = 0.01;
do {
printf("How much change is owed?: ");
owed = GetFloat();
} while(owed <= 0);
change = round(owed * 100);
}
I am using this command to compile my code:
clang -o greedy greedy.c -lcs50
The following should work when you compile:
clang -o greedy greedy.c -lcs50 -lm
This links the math library for the compiler.

Ansi C: Long Double Variable Printing Out to a Value of 0.000000 [duplicate]

This question already has answers here:
gcc: printf and long double leads to wrong output. [C - Type conversion messes up]
(5 answers)
Closed 7 years ago.
When I run the Ansi C program below, a value of "0.000000" is printed out. Does anyone know why the value "561.308000" is not being printed out? I am using Dev-C++ to run the program, and the compiler I am using is: Mingw port of GCC (GNU Compiler Collection), version MSVCRT 2.95.2-1.
#include <stdio.h>
#include <stdlib.h>
main()
{
long double x = 561.308;
printf("%Lf",x);
}
Compiling with gcc (GCC) 4.8.1 on Windows 7 64bit with this command is worked. (Add -std=c99 to the option to have the compiler work with C99)
gcc -Wall -Wextra -std=c99 test.c -o test.exe
the zero might be the return value of the main()
it is printed
Could you try :
printf("Answer is : %Lf",x);
to see whether you got 0.000000 or Answer is: 0.0000
And how about
printf("Answer is : %Lf", 561.38888);
I have this:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
long double x = 561.308;
printf("%Lf",x);
return 0;
}

Output is Mistakenly Zero in c program

I am learning C I decided to make a file that would calculate the surface area and volume of a sphere once its radius was given by the user. However, when I tried compiling the program by typing in:
gcc Sphere.c -lm -o Sphere
and I enter the radius when I am prompted to, the SA and V output is always zero. What's going on here? I may be overlooking something simple. Here is my code:
#include <stdio.h>
#include <math.h>
double main()
{
double rad;
double sa, vol;
const double pi = 3.141592654;
printf("enter the radius of the sphere: \n");
scanf("%f", &rad);
vol = (4.0/3.0)* pi *(pow(rad,3));
sa = 4.0 * pi * (pow(rad,3));
printf("Volume of sphere is: %.3f", vol);
printf("\n Surface area of sphere is: %.3f", sa);
return 0;
}
You are using the wrong scanf() specifier.
The correct specifier for double is "%lf", and scanf() returns a value which you are ignoring causing possibly undefined behavior.
Also, main() does not have the signature you used, it returns a int, not a double or anything else, so your code would work if you fix at as follows
#include <stdio.h>
#include <math.h>
int main(void)
{
double rad;
double sa, vol;
const double pi = 3.141592654;
printf("enter the radius of the sphere: \n");
if (scanf("%lf", &rad) == 1)
{
vol = (4.0/3.0)* pi *(pow(rad,3));
sa = 4.0 * pi * (pow(rad,3));
printf("Volume of sphere is: %.3f", vol);
printf("\n Surface area of sphere is: %.3f", sa);
}
else
fprintf(stderr, "Invalid Input\n");
return 0;
}
Also, vim is a text editor, gcc is a compiler, and you are invoking the compiler with the least diagnostic that is possible, I recommend
gcc -Wall -Werror -g3 -O0 Sphere.c -o Sphere -lm
which will let you know about silly mistakes that you might somtimes make.
Edit:
As noticed by #A.S.H in the comments below, there is a mistake with the surface area formula it has to be
A = 4πr2
you have
A = 4πr3
which is wrong.

Sqrt function not working [duplicate]

This question already has answers here:
Undefined reference to sqrt (or other mathematical functions)
(5 answers)
Closed 5 years ago.
So I'm trying to make a program that calculates the quadratic formula, but when I try to compile the code, I get the following:"undefined reference to sqrt"
But I tried defining sqrt via math.h and 2 other times in the code.
I have attached my code
Any help would be greatly appreciated
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double sqrt(double);
int main (void) {
double sqrt(double);
int a,b,c;
double discriminant,squarerootofdis,root1, root2;
printf("Please enter the coefficient of x^2:");
scanf("%d",&a);
printf("Please enter the coefficient of x:");
scanf("%d",&b);
printf("Please enter the integer value of the ploynomial:");
scanf("%d",&c);
if (a==0 && b==0)
{printf("This case is extremely degenerate");}
else if (a==0 && b!=0)
{root1=-c/b;
printf("Degenerate one real root: %lf\n",root1);}
else{
discriminant = ((b*b)-(4*a*c));
squarerootofdis = sqrt(discriminant);
root1 = (squarerootofdis-b)/(2*a);
root2 = (-squarerootofdis-b)/(2*a);
if (discriminant>0)
printf("Two real roots: %lf\n %lf\n", root1, root2);
else if (discriminant == 0)
printf("Degenerate one real root: %lf\n",root1);
else if (discriminant<0)
printf("Two complex roots: %lf\n %lf\n", root1, root2);
}
}
To use the sqrt function (or any function defined in math.h), you'll have to link the m library:
~$ gcc -lm yourcode.c -o program
Did you compile with -lm linked?
Header file will provide the decalration to the sqrt() function. To have the definition, you need to link with the math library consisting of the function definition.
Example:
gcc test.c -o output -lm
Please use the below command
gcc test.c -lmath

Resources