防止头文件的重复包含
生活随笔
收集整理的這篇文章主要介紹了
防止头文件的重复包含
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? ? ? ? ? ? ? ?防止頭文件重復(fù)包含的宏想必大家都清楚,#ifndef#define#endif就是干這個用的,面試中也考過。我也是知道這個宏的作用,今天我們就來實戰(zhàn)測試一下,網(wǎng)上說的那是別人的東西,只有自己測試過出結(jié)果的才是自己的東西。
[xxx@localhost test]$ ls a.h test.c test.h [xxx@localhost test]$ cat a.h #ifndef A_H #define A_Hint a=1;#endif [xxx@localhost test]$ cat test.h #ifndef TEST_H #define TEST_H#include"a.h" void func(int a);#endif [xxx@localhost test]$ cat test.c #include<stdio.h> #include"test.h" #include"a.h"int main() {func(a);return 0; }void func(int a) {a++;printf("a=%d\n",a); } [xxx@localhost test]$ #ifndef#define#endif是被用在.h文件中不是.c文件中。test.h包含了a.h ,test.c同時包含了test.h和a.h,那test.c包含了2個a.h,即被重復(fù)包含了,這時候就要加上宏。最好2個頭文件都要加,正常來說每個頭文件都要加上這個宏防止被重復(fù)包含。
總結(jié)
以上是生活随笔為你收集整理的防止头文件的重复包含的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3次握手中的最后一个ACK服务端收到了吗
- 下一篇: big endian and littl