![]() ![]() |
|
奇怪的返回:ask:ask | |
作者:佚名 文章来源:不详 点击数 更新时间:2008/4/18 13:57:18 文章录入:杜斌 责任编辑:杜斌 | |
|
|
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 怎么会打印出一个奇怪的数 我的要求是只返回x的值。 pri() ??? return pro() 什么类型。。。。。 pri() /* 应该有返回值 如: int pri(int a) {int x; /* 这个x没有初始化,值是不一定的 */ return x; } 而且,你把test.h放在/usr/include下面实在是奇怪,应该放在test.c同一目录下,在test.c里用 #include "test.h" 包含这个文件 另外,一般 .h 文件是不包含函数定义的,只有函数声明 不知道你正在学C语言的哪一部分,在试验什么特性 如果没有指定的话 c中默认表示返回int值 学到了函数,我看见/usr/include/*里文件全是英文的,我就想自已写个试试有同样的效果吗? 要返回X的值 在同一目录下写3个文件, test.h 代码: #ifndef TEST_H#define TEST_Hint pri(int);#endif /* TEST_H */ test.c 代码: #include "test.h"intpri(int a){ return(a * 2);} main.c 代码: #include <stdio.h>#include "test.h"intmain(void){ int x; x = pri(5); printf("x = %d\n", x); exit(0);} |
|
![]() ![]() |