爲什麼沒有參數的函數(與實際函數定義相比)會編譯?

本文翻譯自:Why does a function with no parameters (compared to the actual function definition) compile?

I've just come across someone's C code that I'm confused as to why it is compiling. 我剛剛遇到了某人的C代碼,但對於爲什麼編譯它感到困惑。 There are two points I don't understand. 我不明白兩點。

First, the function prototype has no parameters compared to the actual function definition. 首先,函數原型與實際函數定義相比沒有參數。 Second, the parameter in the function definition does not have a type. 其次,函數定義中的參數沒有類型。

#include <stdio.h>

int func();

int func(param)
{
    return param;
}

int main()
{
    int bla = func(10);    
    printf("%d", bla);
}

Why does this work? 爲什麼這樣做? I have tested it in a couple of compilers, and it works fine. 我已經在幾個編譯器中對其進行了測試,並且工作正常。


#1樓

參考:https://stackoom.com/question/wXCM/爲什麼沒有參數的函數-與實際函數定義相比-會編譯


#2樓

In C func() means that you can pass any number of arguments. 在C中func()表示您可以傳遞任意數量的參數。 If you want no arguments then you have to declare as func(void) . 如果不需要參數,則必須聲明爲func(void) The type you're passing to your function, if not specified defaults to int . 如果未指定,則傳遞給函數的類型默認爲int


#3樓

If the function declaration has no parameters ie empty then it is taking unspecified number of arguments. 如果函數聲明沒有參數,即爲空,那麼它將使用未指定數量的參數。 If you want to make it take no arguments then change it to: 如果要使其不帶任何參數,則將其更改爲:

int func(void);

#4樓

  • The empty parameter list means "any arguments", so the definition isn't wrong. 空的參數列表表示“任何參數”,因此定義沒有錯。
  • The missing type is assumed to be int . 缺少的類型假定爲int

I would consider any build that passes this to be lacking in configured warning/error level though, there's no point in being this allowing for actual code. 我會認爲,任何通過此設置的構建都缺少配置的警告/錯誤級別,因此沒有必要考慮實際代碼。


#5樓

int func(); is an obsolescent function declaration from the days when there was no C standard, ie the days of K&R C (before 1989, the year the first "ANSI C" standard was published). 是從沒有C標準的日子開始的過時函數聲明,即K&R C的日子(1989年之前,第一個“ ANSI C”標準發佈的年份)。

Remember that there were no prototypes in K&R C and the keyword void was not yet invented. 請記住, K&R C沒有原型,關鍵字void尚未被髮明出來。 All you could do was to tell the compiler about the return type of a function. 您所能做的就是告訴編譯器函數的返回類型 The empty parameter list in K&R C means "an unspecified but fixed" number of arguments. K&R C中的空參數列表表示“數量未指定但固定”的參數。 Fixed means that you must call the function with the same number of args each time (as opposed to a variadic function like printf , where the number and type can vary for each call). 固定表示每次調用函數時必須使用相同數量的args(這與可變參數函數(如printf )不同,在每次調用中,函數的數量和類型都可能不同。

Many compilers will diagnose this construct; 許多編譯器會診斷這種構造。 in particular gcc -Wstrict-prototypes will tell you "function declaration isn't a prototype", which is spot on, because it looks like a prototype (especially if you are poisoned by C++!), but isn't. 特別是gcc -Wstrict-prototypes會告訴您“函數聲明不是原型”,因爲它看起來像原型(特別是如果您被C ++毒害!),但事實並非如此。 It's an old style K&R C return type declaration. 這是舊式的K&R C返回類型聲明。

Rule of thumb: Never leave an empty parameter list declaration empty, use int func(void) to be specific. 經驗法則:切勿將空的參數列表聲明留​​空,請使用int func(void)進行具體說明。 This turns the K&R return type declaration into a proper C89 prototype. 這會將K&R返回類型聲明轉換爲正確的C89原型。 Compilers are happy, developers are happy, static checkers are happy. 編譯器很高興,開發人員很高興,靜態檢查程序很高興。 Those mislead by^W^Wfond of C++ may cringe, though, because they need to type extra characters when they try to exercise their foreign language skills :-) 但是,那些受C ++的^ W ^ Wong誤導的人可能會畏縮,因爲在嘗試鍛鍊外語技能時,他們需要鍵入額外的字符:-)


#6樓

All the other answers are correct, but just for completion 所有其他答案都是正確的,但僅是爲了完成

A function is declared in the following manner: 以以下方式聲明函數:

  return-type function-name(parameter-list,...) { body... } 

return-type is the variable type that the function returns. return-type是函數返回的變量類型。 This can not be an array type or a function type. 不能是數組類型或函數類型。 If not given, then int is assumed . 如果未給出,則假定爲int

function-name is the name of the function. function-name函數的名稱

parameter-list is the list of parameters that the function takes separated by commas. parameter-list是函數採用的參數列表,以逗號分隔。 If no parameters are given, then the function does not take any and should be defined with an empty set of parenthesis or with the keyword void. 如果未提供任何參數,則該函數將不接受任何參數,並且應使用空括號或關鍵字void定義該函數。 If no variable type is in front of a variable in the paramater list, then int is assumed . 如果參數列表中的變量之前沒有變量類型,則假定爲int Arrays and functions are not passed to functions, but are automatically converted to pointers. 數組和函數不傳遞給函數,而是自動轉換爲指針。 If the list is terminated with an ellipsis (,...), then there is no set number of parameters. 如果列表以省略號(,...)終止,則沒有設置的參數數量。 Note: the header stdarg.h can be used to access arguments when using an ellipsis. 注意:使用省略號時,頭stdarg.h可用於訪問參數。

And again for the sake of completeness. 再次爲了完整性。 From C11 specification 6:11:6 (page: 179) 從C11規範6:11:6 (頁面:179)

The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature . 使用帶空括號的函數聲明符 (不是原型格式的參數類型聲明符) 是過時的功能

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章