最近刚学C语言,请问赋值的问题

来源:百度知道 编辑:UC知道 时间:2024/05/12 12:24:41
#include"stdio.h"
#define Q 3.14
main()
{
int a,b,c;
printf("please input a and b"); 输出的值和实际的不同
scanf("%d,%d",&a,&b);
c=Q*a*b;
printf("yhe numble is %d",c);
getch();
}
在编程序中,许多程序的输出和预期都不同,不知道是为什么啊?
我是用WIN-TC的,去掉GETCH();的话结果就无法停留了

/* 我对你的程序做了如下修改和说明, 你看了就明白了 */
#include "stdio.h"
#define Q 3.14
main() {
int a,b;
float c; /* c 改为实数型 */
printf("please input a and b: "); /* 我给你加了一个冒号和空格, 运行时更清晰 */
scanf("%d,%d",&a,&b); /* 输入数值时, 必须用逗号分隔 */
c=Q*a*b; /* c 改为实数型 , 计算结果就不会像先前一样转换为整型 */
printf("The numble is %f\n",c); /* T 错打成了 y; c 变成了实数型, 所以对应的输出格式要由 %d 换成 %f, 最后加了一个 \n 免得多次运行结果挤在一行. */
getch();
}

/*
你原程序的运行结果示例:
please input a and b1,2
yhe numble is 6

修改后:
please input a and b: 1,2
The numble is 6.280000
*/

getch();
这个去掉

3.14是个浮点数,所以至少应该将C定义为float类型。

getch()不需要去掉,但是最好加上#include <conio.h>
像这样:

#include"stdio.h"

#include <conio.h>

#define Q 3.14
main()
{
int a,b;
float c;
printf("please input a and b");
scanf("%d,%d",&a,&b);<