Skip to content

Commit 8501280

Browse files
committed
4.6 Include guard added
1 parent c0853fd commit 8501280

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// in1.h
3+
// CPP-Notes
4+
//
5+
// Created by Akshay Raj Gollahalli on 30/05/16.
6+
// Copyright © 2016 Akshay Raj Gollahalli. All rights reserved.
7+
//
8+
9+
#include "in2.h" // We are including in2.h here
10+
11+
int b = 20;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// in2.h
3+
// CPP-Notes
4+
//
5+
// Created by Akshay Raj Gollahalli on 30/05/16.
6+
// Copyright © 2016 Akshay Raj Gollahalli. All rights reserved.
7+
//
8+
9+
#ifndef in2_h // Check if in2.h is not defined, if so
10+
#define in2_h // define it.
11+
12+
int a = 10;
13+
14+
#endif /* in2_h */
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// CPP-Notes
3+
//
4+
// Created by Akshay Raj Gollahalli on 23/05/16.
5+
// Copyright © 2016 Akshay Raj Gollahalli. All rights reserved.
6+
//
7+
8+
#include <cstdio>
9+
#include "in1.h"
10+
#include "in2.h"
11+
using namespace std;
12+
13+
14+
int main(int argc, char ** argv) {
15+
16+
printf("a = %d, b = %d\n",a,b);
17+
18+
return 0;
19+
}
20+

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ C++ Notes
3636
- [4.4 Macro expansion](#44-macro-expansion)
3737
- [4.5 Problems with Macro's](#45-problems-with-macros)
3838
- [4.6 Line continuation](#46-line-continuation)
39+
- [4.6 Include guard](#46-include-guard)
3940

4041
<!-- /TOC -->
4142

@@ -866,3 +867,16 @@ If you want to use complex macros you can use `line continuation` by add `\` at
866867
printf("%d\n",i++); \
867868
} while (i < 3);
868869
```
870+
871+
### 4.6 Include guard
872+
873+
See [4_6_Include_Guard](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_6_Include_Guard)
874+
875+
There might be a situation where you might have to define a header file in another header file and when called in a C++ file you might include both header files. When you compile this you will have a `Build fail`, to over come this we have to include something called as `Include guard`. It looks something like this
876+
877+
```cpp
878+
#ifndef _HEADERNAME_H
879+
#define _HEADERNAME_H
880+
...
881+
#endif
882+
```

0 commit comments

Comments
 (0)