如果我們在 C 程式中兩次包含標頭檔案,會發生什麼情況?
C 標頭檔案中包含一些預定義函式。例如,stdio.h 標頭檔案中定義了 printf() 和 scanf() 函式。
C 中的每個標頭檔案都包含不同的預定義函式,使程式容易理解。
當一個頭檔案在 C 程式中包含兩次時,第二次會被忽略。實際上,一個井號 (#) 加在一個頭檔案前面(稱為 include),確保了它在編譯過程中只包含一次。
示例 1
以下是一個 C 程式,用於計算三個數字的平均值 -
#include<stdio.h>
#include<stdio.h> //header file included twice ,ignored by compiler
main(){
int a,b,c,d;
float avg;
printf("Enter values for a,b,c:");
scanf("%d%d%d",&a,&b,&c);
d=a+b+c;
avg=d/3;
printf("Average avg=%f",avg);
}輸出
當執行以上程式時,它將產生以下結果 -
Enter values for a,b,c:3 3 3 Average avg=3.000000
示例 2
考慮另一個用於標頭檔案的 C 程式 -
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
#include<stdlib.h>
//header file included twice ,ignored by compiler
main(){
int a,b,c;
printf("Enter values for a,b:");
scanf("%d%d",&a,&b);
c=a+b;
printf("sum=%d",c);
}輸出
當執行以上程式時,它將產生以下結果 -
Enter values for a,b:2 4 sum=6
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP