c - void 影响程序的输出

标签 c linux gcc

我正在调用一个打印值的函数,但是当在调用之前添加 void 时,该函数没有给出正确的(或任何)输出。

我尝试了各种方法

#include <stdio.h> 

void func1(); 
void func2(); 

void func1() 
{ 
    printf("Inside func1()\n"); 
} 

void func2() 
{ 
    printf("Inside func2()\n"); 
} 

int main() 
{ 
    void func1(); 
    void func2(); 
    printf("Inside main()\n"); 

    return 0; 
} 

输出是:-

Inside main 

在调用 func1func2 之前移除 void 时,输出会发生变化。

#include <stdio.h> 

void func1(); 
void func2(); 

void func1() 
{ 
    printf("Inside func1()\n"); 
} 

void func2() 
{ 
    printf("Inside func2()\n"); 
} 

int main() 
{ 
    func1(); 
    func2(); 
    printf("Inside main()\n"); 

    return 0; 
} 

输出是:-

Inside func1
Inside func2
Inside main 

谁能解释一下 void 是如何影响期望的输出的?

最佳答案

main 中的语句 void func1(); 是一个函数声明。这是运行时的空操作。

func1(); 实际上调用了函数。

这就是语言语法的运作方式,仅此而已。如果你考虑一下,它是相当聪明的。

关于c - void 影响程序的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56867398/

相关文章:

c - AVR MCU 崩溃,代码中没有逻辑错误。我应该检查什么?

c - 用 C 实现 Malloc

c - 限制 C 中寄存器的使用

c - MinGW/Eclipse C 构建问题 : cannot find dll

c - 不使用 % 和/运算符的 5 的整除率

java - 在java程序中执行bash命令

linux - 为什么 syslog 文件中的内核日志消息(或那些重定向到终端的消息)正好落后一个 'message'?

Python 内存错误(Unix 与 Windows)

重定位 vector 表的C代码

c - 修复 "An invalid parameter was passed to a function that considers invalid parameters fatal"