QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1939|回复: 15

编译C语言时遇到了问题,求助!

[复制链接]
发表于 2005-10-17 22:20:17 | 显示全部楼层 |阅读模式
小弟想在linux下学C语言,可是编译的时候不成功.是不是函数名不对啊?还是调用函数的方法错了?请各位大哥大姐帮忙啊!
下面是小代码:
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c,s,area;
scanf("%f,%f,%f",&a,&b,&c);
s=1.0/2*(a+b+c);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("a=%7.2f,b=%7.2f,c=%7.2f,s=%7.2f\n",a,b,c,s);
printf("area=%7.2f\n",area);
}
结果:
/tmp/ccIms7yH.o(.text+0x90): In function `main':
fffff.c: undefined reference to `sqrt'
collect2: ld 返回 1
怎么回事啊?
发表于 2005-10-17 23:43:19 | 显示全部楼层
main() -------->没有定义类型 正确的int main()
sqrt  math.h 没有定义 要定义一下 #define sqrt(x) x*x
回复

使用道具 举报

发表于 2005-10-17 23:46:28 | 显示全部楼层
#include <stdio.h>
#include <stdilb.h>
#include <math.h>
#define sqrt(x) x*x

int main() {
{
float a,b,c,s,area;
scanf("%f,%f,%f",&a,&b,&c);
s=1.0/2*(a+b+c);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("a=%7.2f,b=%7.2f,c=%7.2f,s=%7.2f\n", a, b, c, s);
printf("area=%7.2f\n", area);
return(0);
}
回复

使用道具 举报

 楼主| 发表于 2005-10-18 10:03:51 | 显示全部楼层
谢谢,成功了
回复

使用道具 举报

 楼主| 发表于 2005-10-18 10:15:06 | 显示全部楼层
[quote:45aa4990cf="0xfffff"]main() -------->没有定义类型 正确的int main()
sqrt  math.h 没有定义 要定义一下 #define sqrt(x) x*x[/quote]

"#define sqrt(x) x*x"是什么意思?
回复

使用道具 举报

 楼主| 发表于 2005-10-18 10:17:33 | 显示全部楼层
是不是所有类似的情形都要这么定义一下?拜托举个例子好吗?
回复

使用道具 举报

发表于 2005-10-18 12:24:27 | 显示全部楼层
#define 是预处理宏
也不是所有 这个方便你定义一些简单的算法
也可以用定义函数表示
int sqrt(x){
int x;
x=x*x;
}
回复

使用道具 举报

 楼主| 发表于 2005-10-18 13:14:19 | 显示全部楼层
我照你的代码编译了一下,是可以编译了,可结果不对啊.
#include<stdio.h>
#include<math.h>
#define sqrt(x) x*x
main()
{
float a,b,c,s,area;
scanf("%f,%f,%f",&a,&b,&c);
s=1.0/2*(a+b+c);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("a=%7.2f,b=%7.2f,c=%7.2f,s=%7.2f\n",a,b,c,s);
printf("area=%7.2f\n",area);
}
回复

使用道具 举报

发表于 2005-10-18 13:29:16 | 显示全部楼层
#include<stdio.h>
#include<math.h>
#define sqrt(x) x*x
main()
{
float a,b,c,s,area;
int x;
scanf("%f,%f,%f",&a,&b,&c);
s=1.0/2*(a+b+c);
x=s*(s-a)*(s-b)*(s-c);
area=sqrt(x);
printf("a=%7.2f,b=%7.2f,c=%7.2f,s=%7.2f\n",a,b,c,s);
printf("area=%7.2f\n",area);
}
回复

使用道具 举报

发表于 2005-10-18 13:31:06 | 显示全部楼层
#include<stdio.h>
#include<math.h>

main()
{
float a,b,c,s,area;
scanf("%f,%f,%f",&a,&b,&c);
s=1.0/2*(a+b+c);
x=s*(s-a)*(s-b)*(s-c);
area=sqrt(x);
printf("a=%7.2f,b=%7.2f,c=%7.2f,s=%7.2f\n",a,b,c,s);
printf("area=%7.2f\n",area);
}

int sqrt(x){
int x;
x=x*x;
}
回复

使用道具 举报

发表于 2005-10-18 13:32:55 | 显示全部楼层
你有没有 输入错误....
回复

使用道具 举报

 楼主| 发表于 2005-10-18 13:57:18 | 显示全部楼层
sqrt 是开根号啊...我输3,4,5进去,得到1296..
回复

使用道具 举报

 楼主| 发表于 2005-10-18 13:59:32 | 显示全部楼层
你在自己机上编译试试看行不行?
回复

使用道具 举报

发表于 2005-10-18 16:51:51 | 显示全部楼层
你慢慢试吧    
回复

使用道具 举报

发表于 2005-10-18 16:55:11 | 显示全部楼层
你这是C还是C++我看了有点 麻痹
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-10-1 21:06 , Processed in 0.084539 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表