简体   繁体   English

为什么在函数定义中对于函数自变量没有强制提到“ int”数据类型?

[英]Why is `int` datatype not mandatory to be mentioned for function arguments in the function definition?

Recently I went through some code similar to this: (The code is proprietary, and hence adding a similar one) 最近,我经历了一些与此类似的代码:(该代码是专有的,因此添加了类似的代码)

#include<stdio.h>

void test_it(var)
{
    printf("%d\n",var);
}

int main()
{
    test_it(67);
    return 0;
}

The arguments of test_it do not have datatype mentioned. test_it的参数没有提到数据类型。

I compiled it as gcc test_it.c ... : Surprisingly No Warnings/Error 我将其编译为gcc test_it.c ...:令人惊讶地没有警告/错误

Again I compiled using: gcc -Wall test_it.c ... : No Warnings/Error yet again 我再次使用以下命令进行编译: gcc -Wall test_it.c ...:再次没有警告/错误

(Getting more aggressive now...) (现在变得更具侵略性...)

I compiled it again using: gcc -Wall -Wextra test_it.c ... : 我使用gcc -Wall -Wextra test_it.c ...再次编译它:
warning: type of 'var' defaults to 'int' finally I got the warning. warning: type of 'var' defaults to 'int'最后我得到了警告。

I tried using multiple arguments as: 我尝试使用多个参数作为:

void test_it(var1, var2)
{
    printf("%d\n%d\n",var1, var2);
}

int main()
{
    test_it(67,76);
    return 0;
}

Same beahavior!! 同样的行为!!

Also I tried this: 我也试过这个:

void test_it(var)
{
    printf("%d\n",var);
}

main()   // Notice that no `int` there
{
    test_it(67);
    return 0;
}

This code gave warning with -Wall option only. 此代码仅使用-Wall选项给出警告。

So my question is why the int datatype is not mandatory for function arguments in function definition? 所以我的问题是为什么int数据类型对于函数定义中的函数参数不是必需的?

EDIT: 编辑:

Rewording the question: 重述问题:

Why gcc doesn't give warning with -Wall in the case of omitting datatype of function arguments, but gives warning for omitting the function return type? 为什么在省略函数参数的数据类型的情况下, gcc不会用-Wall发出警告,而对于省略函数返回类型却给出警告? Why does it ignore it in the first case? 为什么在第一种情况下忽略它?

In C89, the default type is assumed to be int . 在C89中,默认类型假定为int This ( is valid in C89 ), however the default type rule has been abandoned in C99 . 这( 在C89中有效 ),但是在C99中已放弃默认类型规则。 See the difference: 看到不同:

C89 - Compiles fine C89- Compiles fine

C99 prog.c:3:6: error: type of 'var' defaults to 'int' C99 prog.c:3:6: error: type of 'var' defaults to 'int'

Try to compile with the -std=c99 flag. 尝试使用-std=c99标志进行编译。

By default the functions arguements are of type int in C89. 默认情况下,函数争论在C89中为int类型。 So the code executes fine. 因此代码执行得很好。
You can go through this related question. 您可以解决这个相关问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM