【C++】-面试问题-int f()与int f(void)有区别吗?

一、答案解析

(1)若在C语言编译器中,

int f():

  表示返回值int,参数为任意多个;

int f(void):

  表示返回值为int,不接收任何的参数。

(2)若在C++编译器中,两种解法没有任何区别。表示返回值为int的无参函数。

二、实例分析

我们可以使用一个实例来阐明两者的区别:

#include <stdio.h>

struct Student
{
	const char* name;
	int age;
};

f(i)
{
	printf("i = %d\n", i);
}

g()
{
	return 5;
}


int main(int argc, char *argv[])
{
	Student s1 = { "Delphi", 30 };
	Student s2 = { "Tang", 30 };

	f(10);

	printf("g() = %d\n", g(1, 2, 3, 4, 5));

	return 0;
}

(1)在C++编译器中,编译

可见,C++不支持默认的返回值;函数 g()不接受5个参数

(2) 在C编译器中,编译

编译出错,Student不是一个类型

在结构体前面添加:
 

typedef struct _tag_student Student

 此时,Student是一个类型,编译通过

说明C语言允许默认类型.

参考资料:

https://item.taobao.com/item.htm?spm=a230r.1.14.19.1da82418BZtdkY&id=559166901112&ns=1&abbucket=16#detail 

猜你喜欢

转载自blog.csdn.net/qq_40416052/article/details/82083755