【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 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值