当我们在函数中不指定参数的数据类型并在调用函数时将参数传递给它时,会发生什么?
看下面的程序。
int main()
{
char a=65, ch ='c';
printit(a,ch);
}
printit(a,ch)
{
printf("a=%d ch=%c",a,ch);
}
即使函数“printit()”中未指定参数的数据类型,结果也会显示在 printf 上。 当我用 gcc 编译并运行它时,我看到了正确的答案。为什么? C 中是否不需要指定参数的数据类型? 在上面所示的情况下,参数的默认数据类型是什么?
Look at the following program.
int main()
{
char a=65, ch ='c';
printit(a,ch);
}
printit(a,ch)
{
printf("a=%d ch=%c",a,ch);
}
Even if the data type of the arguments is not specified in the function 'printit()', the result is shown on printf. I see correct answer when i compile it with gcc and run it.Why? Is it not necessary to specify the data type of arguments in C ? What is the default datatype of argument taken in the case shown above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您没有为
printit()
指定原型,编译器会编写隐式声明:当以后的编译器看到
printit()
函数的定义没有参数类型时,它使用该隐式声明。这是非常危险的技术 - 您基本上禁止对此函数进行类型检查。
Because you don't specify a prototype for
printit()
, compiler makes up implicit declaration:When later compiler sees the definition of
printit()
function without types for arguments, it uses that implicit declaration.It is very dangerous technique - you basically prohibit type checking for this function.
C 中假定的唯一默认数据类型是
int
,如上面的代码所示。较新版本的 C++ 禁止隐式数据类型,并且较新的 C++ 编译器拒绝编译上述代码。
The only default datatype assumed in C is
int
as in the code above.Newer versions of C++ prohibit implicit data typing and newer C++ compilers refuse to compile the code above.