【问题标题】:C program compiles but no outputC程序编译但没有输出
【发布时间】:2013-03-10 02:53:36
【问题描述】:

我正在尝试学习在 C 中创建头文件并将其包含在我的 main.c func() 中。我创建了一个简单的 tut1.c 文件,其中包含名为 call() 的函数和一个 tut1.h 头文件,其中包含名为 call() 的 tut1.c 函数。就是这样,现在我在 linux fedora 上使用 eclipse Juno for C/C++。我没有得到任何编译错误,但代码不会输出?我徒劳地尝试了控制台和日食。你能检查一下吗?谢谢

---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()中声明一个不存在的函数(tut1())?
  • 没有输出,因为你没有调用任何东西——只是声明一个函数,然后返回 0。
  • 请注意,此程序还调用未定义的行为:指针和地址必须使用%p格式说明符打印,并且参数必须为(投射到)void *
  • 嗨 Jens,我理解 %p 但在我上面的示例中,“论点必须是 void* 的大小写”是什么意思?

标签: c header-files


【解决方案1】:

您没有看到任何内容,因为您没有从 main() 函数调用 call() 函数。

main() 函数是您运行程序时的默认入口点,即在执行期间被调用的第一个函数。

要执行函数call(),您需要从main() 调用它,如下所示:

int main (void)

{
    int result = call();

    return 0;
}

顺便说一句,main() 中的这一行 int tut1(void); 只是声明了一个函数,您似乎没有在任何地方定义它。所以我在上面显示的代码中删除了它。

【讨论】:

  • 谢谢大家。是的,我意识到我的错误。非常感谢您的反馈,每一个都很有价值。干杯
猜你喜欢
  • 1970-01-01
  • 2014-11-12
  • 2020-09-05
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
相关资源
最近更新 更多