Codeblocks 不打印特定格式

标签 c format printf

#include <stdio.h>
#include <math.h>
int main(void)
{
 double a, b, c, root1, root2;
 printf("Input the coefficient a => ");
 scanf("%lf", &a);
 printf("Input the coefficient b => ");
 scanf("%lf", &b);
 printf("Input the coefficient c => ");
 scanf("%lf", &c);
/* Compute the roots. */
root1 = (- b + sqrt(b*b-4*a*c))/(2*a);
root2 = (- b - sqrt(b*b-4*a*c))/(2*a);
printf("The first root is %8.3f\n", root1);
printf("The second root is %8.3f\n", root2);
return 0;
}

但是,我的输出是

Input the coefficient a => 232
Input the coefficient b => 23
Input the coefficient c => 2
The first root is      nan
The second root is      nan 

我是个初学者,格式有问题吗? 使用代码块,用 C 语言编写。

最佳答案

试试这个:

#include <stdio.h>
#include <math.h>

int main(void)
{
 double a, b, c, root1, root2;
 double temp;
 printf("Input the coefficient a => ");
 scanf("%lf", &a);
 printf("Input the coefficient b => ");
 scanf("%lf", &b);
 printf("Input the coefficient c => ");
 scanf("%lf", &c);
/* Compute the roots. */
temp = b*b-4*a*c;
if (temp >= 0) {
    root1 = (- b + sqrt(temp))/(2*a);
    root2 = (- b - sqrt(temp))/(2*a);
    printf("The first root is %8.3f\n", root1);
    printf("The second root is %8.3f\n", root2);  
} else {
    printf("There is no root!\n");
}

return 0;
}

remember : load math library like this -> gcc "fileName" -lm

关于Codeblocks 不打印特定格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39847198/

相关文章:

c - 如何在 jni 中访问传递给 c 的 JSON 数据?

c - GNU 欺骗 : function registered with scm_c_define_gsubr: how can i handle an optional parameter?

c - 写系统调用,字节数限制是多少?

java - 未知格式转换异常错误?

python - LogFormatter 具有稀疏刻度标签且没有科学记数法

c - 为什么 %c 打印 '?',它的含义是什么

c - 在 C 中打印十六进制字符

c - OpenGL:移动相机时如何使光线静止?

c# - 正则表达式格式无法按预期工作

linux - 是否有一个程序可以像 printf 一样以整个文件作为输入?