LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1041|回复: 10

奇怪的返回:ask :ask

[复制链接]
发表于 2003-10-25 19:52:13 | 显示全部楼层 |阅读模式
test.h是我写的一个函数吧(不知道是不是)
请看:
linux# gcc test1.c
linux# ./a.out
-1073743380
linux# cat test1.c
#include <test.h>
main()
{
int a=2,b;
b=pri(a);
printf("%d\n",b);
}


linux# cat /usr/include/test.h
pri()
{int x;
        return x;
}

linux#
我就只是返回了x 怎么会打印出一个奇怪的数:ask :ask 我的要求是只返回x的值。
发表于 2003-10-25 20:05:09 | 显示全部楼层
pri()

???

return pro()

什么类型。。。。。
发表于 2003-10-25 20:40:46 | 显示全部楼层
pri() /* 应该有返回值 如: int pri(int a)
{int x; /* 这个x没有初始化,值是不一定的 */
return x;
}

而且,你把test.h放在/usr/include下面实在是奇怪,应该放在test.c同一目录下,在test.c里用 #include "test.h" 包含这个文件
另外,一般 .h 文件是不包含函数定义的,只有函数声明

不知道你正在学C语言的哪一部分,在试验什么特性
发表于 2003-10-25 20:43:32 | 显示全部楼层
pri(a)

pri()

函数参数是????

太。。。。。。。。。。。
发表于 2003-10-25 22:08:28 | 显示全部楼层
如果没有指定的话
c中默认表示返回int值
 楼主| 发表于 2003-10-25 22:12:23 | 显示全部楼层
谢谢,刷新就多了无版主的回贴了。呵呵~~~

学到了函数,我看见/usr/include/*里文件全是英文的,我就想自已写个试试有同样的效果吗?

我要返回X的值,小妹很莱,不懂怎么写。请问具体怎么写:ask:ask
发表于 2003-10-25 23:55:54 | 显示全部楼层
在同一目录下写3个文件,
test.h

  1. #ifndef TEST_H
  2. #define TEST_H

  3. int pri(int);

  4. #endif /* TEST_H */
复制代码

test.c

  1. #include "test.h"

  2. int
  3. pri(int a)
  4. {
  5.   return(a * 2);
  6. }
复制代码

main.c

  1. #include <stdio.h>
  2. #include "test.h"

  3. int
  4. main(void)
  5. {
  6.   int x;

  7.   x = pri(5);
  8.   printf("x = %d\n", x);

  9.   exit(0);
  10. }
复制代码

用 gcc main.c test.c -o test 编译
发表于 2003-10-26 05:51:51 | 显示全部楼层
编译时也可以这样:

gcc main.c test.c -o test

<=>

make main.c test.c -o test
 楼主| 发表于 2003-10-26 11:21:36 | 显示全部楼层
谢谢大家,看来我还有很多不懂的,以后要好好学习。呵呵~~
再次谢谢你们拉
发表于 2003-10-30 10:03:57 | 显示全部楼层
编译时也可以这样:

gcc main.c test.c -o test

<=>

make main.c test.c -o test


是错误的。。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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