当我们在函数中不指定参数的数据类型并在调用函数时将参数传递给它时,会发生什么?

发布于 2024-07-25 14:20:53 字数 285 浏览 9 评论 0原文

看下面的程序。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

本宫微胖 2024-08-01 14:20:54

因为您没有为 printit() 指定原型,编译器会编写隐式声明:

int printit(int, int);

当以后的编译器看到 printit() 函数的定义没有参数类型时,它使用该隐式声明。

这是非常危险的技术 - 您基本上禁止对此函数进行类型检查。

Because you don't specify a prototype for printit(), compiler makes up implicit declaration:

int printit(int, int);

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.

蛮可爱 2024-08-01 14:20:53

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文