我编写了一个小的 c 代码来查找数字的 sqrt。而且我希望程序甚至可以找到负数的 sqrt。我遇到了 complex.h 和几个函数来执行复杂的算术。我已经使用了它们,并且在编译时收到了一些警告消息。代码没有正确计算正数和负数的平方根。
1) printf 和 scanf 用于打印和输入复数的正确格式说明符是什么?请让我知道我到底哪里出错了。我使用的是 gcc 编译器,操作系统是 Ubuntu 12.04 LTS。我附上了代码和输出。
#include <stdio.h>
#include <math.h>
#include <complex.h>
int main()
{
float complex var,op;
printf(" Enter the number : " );
scanf("%lf",&var);
op=csqrt(var);
printf("%lf \n",op);
}
o/p Enter the number : 4
0.011604
Another o/p Enter the number : -4
-0.011604