簡體   English   中英

為什么在函數定義中對於函數自變量沒有強制提到“ int”數據類型?

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

最近,我經歷了一些與此類似的代碼:(該代碼是專有的,因此添加了類似的代碼)

#include<stdio.h>

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

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

test_it的參數沒有提到數據類型。

我將其編譯為gcc test_it.c ...:令人驚訝地沒有警告/錯誤

我再次使用以下命令進行編譯: gcc -Wall test_it.c ...:再次沒有警告/錯誤

(現在變得更具侵略性...)

我使用gcc -Wall -Wextra test_it.c ...再次編譯它:
warning: type of 'var' defaults to 'int'最后我得到了警告。

我嘗試使用多個參數作為:

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

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

同樣的行為!!

我也試過這個:

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

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

此代碼僅使用-Wall選項給出警告。

所以我的問題是為什么int數據類型對於函數定義中的函數參數不是必需的?

編輯:

重述問題:

為什么在省略函數參數的數據類型的情況下, gcc不會用-Wall發出警告,而對於省略函數返回類型卻給出警告? 為什么在第一種情況下忽略它?

在C89中,默認類型假定為int 這( 在C89中有效 ),但是在C99中已放棄默認類型規則。 看到不同:

C89- Compiles fine

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

嘗試使用-std=c99標志進行編譯。

默認情況下,函數爭論在C89中為int類型。 因此代碼執行得很好。
您可以解決這個相關問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM