? 最近閱讀工程代碼的時候,同一個函式,不同場景呼叫時,輸入的實參個數不一樣,但是編譯卻沒有問題,查看函式的定義,相關的C檔案里并沒有給形參指定默認值,這就很奇怪了,
? 最終,發現在函式相關的頭檔案里有給形參指定默認值,這就能解釋通為什么形參和實參個數不一致,編譯能正常通過的問題了,下面是示例代碼,
/*parainput.c 檔案內容*/
#include <stdio.h>
void sum(int a,int b,int c)
{
int result = a + b + c;
printf("result = %d\n",result);
}
/*parainput.h 檔案內容*/
#ifndef _PARAINPUT_H
#define _PARAINPUT_H
void sum(int a,int b=1,int c=2);
#endif
/*main.c 檔案內容*/
#include <stdio.h>
#include "parainput.h"
void test_01(void)
{
int a = 10;
sum(a);
return;
}
void test_02(void)
{
int a = 10,b = 20;
sum(a,b);
}
void test_03(void)
{
int a = 10,b = 20,c = 30;
sum(a,b,c);
}
int main(void)
{
test_01();
test_02();
test_03();
return 0;
}
用G++進行編譯,最終運行結果如下,
result = 13
result = 32
result = 60
--------------------------------
Process exited after 0.006126 seconds with return value 0
請按任意鍵繼續. . .
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/471732.html
標籤:C
上一篇:C++進階實體3--基于STL的演講比賽流程管理系統
下一篇:列舉與聯合體的介紹與實體
