我正在尝试学习在 C 中创建头文件并将其包含在我的 main.c func() 中。我创建了一个简单的 tut1.c 文件,其中包含名为 call() 的函数和一个名为 call() 的外部 tut1.c 函数的 tut1.h 头文件。就是这样,现在我在 linux fedora 上使用 eclipse Juno for C/C++。我没有收到任何编译错误,但代码不会输出?我徒劳地尝试了控制台和 eclipse 。你能检查一下吗?谢谢
---main.c-----
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "tut1.h"
int main (void)
{
int tut1(void);
return 0;
}
-----tut1.c------
#include <stdio.h>
#include <stdlib.h>
#include "tut1.h"
int call (void)
{
int *ptr;
int i;
ptr = &i;
*ptr = 10;
printf ("%d we are printing the value of &i\n", &i);
printf ("%d we are printing the value of *ptr\n", *ptr);
printf ("%d we are printing the value of ptr\n", ptr);
printf ("%d we are printing the value of &ptr\n", &ptr);
getchar();
return 0;
}
----tut1.h----
#ifndef TUT1_H_
#define TUT1_H_
extern int call (void);
#endif
最佳答案
您没有看到任何内容,因为您没有从 main()
函数中调用 call()
函数。
main()
函数是您运行程序时的默认入口点,即在执行期间被调用的第一个函数。
要执行函数 call()
,您需要从 main()
中调用它,如下所示:
int main (void)
{
int result = call();
return 0;
}
顺便说一句,int tut1(void);
在您的 main()
中的这一行只是声明了一个您似乎没有在任何地方定义的函数。所以我在上面显示的代码中删除了它。
关于C程序编译但没有输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15314313/