來自K & R Book on C,我收集了如果函數原型聲明省略參數(如在int foo();
中),則關閉類型和參數檢查並且不會假定關於參數與舊版本的C兼容,因此它不會破壞舊版代碼。函數原型 - 關閉參數檢查
但下面的代碼拋出一個編譯錯誤的原型不匹配:
#include <stdio.h>
void test();
int main(void) {
test(34.5f);
}
void test(float a) {
printf("%f\n", a);
}
錯誤:
C:\***.c:7:6: error: conflicting types for 'test'
void test(float a) {
^
任何解釋嗎?
gcc增加:'注意:具有默認提升的參數類型不能匹配空參數名稱列表聲明',所以'void test(double a)'工作,但'void test(float a)'doesn 「T。因此,似乎「關閉了類型和參數檢查並且沒有任何關於爭論的事情」是過於簡單化了。您應該參考C規範以查看規則的實際內容。 – ikegami
請參閱http://stackoverflow.com/questions/1630631/alternative-kr-c-syntax-for-function-declaration-versus-prototypes某些人可能會認爲這個問題是該問題的重複,並關閉。 –
編譯器期望定義'void test(double a){}',因爲'test(34.5f);'調用涉及_default參數promotions_,如果給定的聲明不作爲原型,就是這種情況。其中一種促銷是「float ---> double」。 –