[英]Turbo C compiler issue, sqrt() function not working with variable arguments
I searched the question similar to my problem Similar problem<\/a> .我搜索了与我的问题相似的问题
Similar<\/a> question 。 But my problem is when using Turbo C compiler v3.0.
但我的问题是使用 Turbo C 编译器 v3.0 时。 Should I have to do some additional work for math.h file?
我应该为 math.h 文件做一些额外的工作吗? please help.
请帮忙。
int main (void){
double result, a;
clrscr();
printf("Enter a # for square root.\n");
scanf("%f",&a);
printf("a = %f\n",a);
result = sqrt(a);
printf("a = %f and square root is %f\n",a, result);
getch();
return 0;
}
For scanf()
, %f
is for float
. 对于
scanf()
, %f
用于float
。 You need to use %lf
for double
: 您需要将
%lf
用于double
:
printf("Enter a # for square root.\n");
scanf("%lf",&a);
This is in contrast to printf()
where type-promotion allows %f
to be used for both float
and double
. 这与
printf()
相反,其中type-promotion允许%f
用于float
和double
。
Try this : 尝试这个 :
scanf("%lf",&a);
or change the variable a to float: 或者将变量a更改为float:
float a;
In addition to using "%lf"
as the scanf
format, you need to have 除了使用
"%lf"
作为scanf
格式之外,您还需要
#include <stdio.h>
#include <math.h>
#include <conio.h> /* I think */
The last one is for the clrscr()
and getch()
calls; 最后一个用于
clrscr()
和getch()
调用; they're non-standard, but I think they're declared in <conio.h>
. 它们是非标准的,但我认为它们是在
<conio.h>
声明的。
Without the #include <math.h>
, the compiler is going to assume that sqrt()
returns an int
result rather than double
. 如果没有
#include <math.h>
,编译器将假设sqrt()
返回int
结果而不是double
。
(An aside: Why do you call clrscr()
? What is the benefit of clearing the screen before doing anything else? The getch()
call isn't strictly necessary either, but on some systems the default method of running a program results in the window being closed as soon as it terminates.) (旁边:为什么要调用
clrscr()
?在执行任何其他操作之前清除屏幕有什么好处? getch()
调用也不是必需的,但在某些系统上运行程序的默认方法会导致窗口一旦终止就关闭。)
Declare variable a in float and variable result in double, and use math.h header file在 float 中声明变量 a,在 double 中声明变量结果,并使用 math.h 头文件
And use to take input ("%f", &a);并用于获取输入("%f", &a); for taking input
接受输入
And then result = sqrt(a);然后结果 = sqrt(a);
and print result in "%lf" format并以“%lf”格式打印结果
it works perfectly in turbo c它在涡轮 c 中完美运行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.