[英]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
類型。 因此代碼執行得很好。
您可以解決這個相關問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.