Skip to content

Commit ae6b9f6

Browse files
SSwiniarskiDusch4593KTom101
authored
New Entry C & C++ Macros (#824)
* Create macros.md * Update preprocessors.md * Update macros.md * Update macros.md * Create macros.md * yarn format * Update content/c/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/c/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/c/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/c/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/c/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/cpp/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/c/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/c/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/cpp/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/c/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/cpp/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/cpp/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/cpp/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/cpp/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/cpp/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/cpp/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/cpp/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/cpp/concepts/macros/macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> * Update content/c/concepts/macros/macros.md Co-authored-by: KTom101 <kyra.thompson@nyu.edu> * Update content/cpp/concepts/macros/macros.md Co-authored-by: KTom101 <kyra.thompson@nyu.edu> * Update macros.md * Update macros.md * yarn format * Update macros.md * Update macros.md Co-authored-by: Brandon Dusch <brandondusch@gmail.com> Co-authored-by: KTom101 <kyra.thompson@nyu.edu>
1 parent fefd54e commit ae6b9f6

File tree

3 files changed

+305
-1
lines changed

3 files changed

+305
-1
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
Title: 'Macros'
3+
Description: 'A macro is a label defined in the source code that is replaced by its value by the preprocessor before compilation.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Command Line'
9+
- 'Developer Tools'
10+
CatalogContent:
11+
- 'learn-c'
12+
- 'paths/computer-science'
13+
---
14+
15+
A **macro** is a label defined in the source code that is replaced by its value by the [preprocessor](https://www.codecademy.com/resources/docs/c/preprocessors) before [compilation](https://www.codecademy.com/resources/docs/c/compiling). Macros are initialized with the `#define` preprocessor command and can be undefined with the `#undef` command.
16+
17+
There are two types of macros: object-like macros and function-like macros.
18+
19+
## Object-Like Macros
20+
21+
These macros are replaced by their value in the source code before compilation. Their primary purpose is to define constants to be used in the code.
22+
23+
**Note**: Macro definitions are not followed by a semicolon `;`.
24+
25+
### Example
26+
27+
In the following example, `PI` is defined as an object-like macro:
28+
29+
```c
30+
#include <stdio.h>
31+
#define PI 3.1416
32+
33+
int main() {
34+
float radius = 3;
35+
float area;
36+
area = PI * radius * radius;
37+
printf("Area is: %f",area);
38+
return 0;
39+
}
40+
```
41+
42+
This example outputs the following:
43+
44+
```shell
45+
Area is: 28.274401
46+
```
47+
48+
## Function-Like Macros
49+
50+
These macros behave like [functions](https://www.codecademy.com/resources/docs/c/functions), in that they take arguments that are used in the replaced code. Note that in defining a function-like macro, there cannot be a space between the macro name and the opening parenthesis.
51+
52+
### Example
53+
54+
In the following example, `AREA` is defined as a function-like macro. Note that other macros can be used in defining a subsequent macro. The inner macro is replaced by its value before the outer macro is replaced.
55+
56+
```c
57+
#include <stdio.h>
58+
#define PI 3.1416
59+
#define AREA(r) r * r * PI
60+
61+
int main() {
62+
float radius = 5;
63+
float result;
64+
result = AREA(radius);
65+
printf("Area is: %f",result);
66+
return 0;
67+
}
68+
```
69+
70+
This example outputs the following:
71+
72+
```shell
73+
Area is: 78.540001
74+
```
75+
76+
## Predefined Macros
77+
78+
C has a number of predefined macros, including the following:
79+
80+
- `__DATE__`: Current date formatted as `MMM DD YYYY`.
81+
- `__TIME__`: Current time formatted as `HH:MM:SS`.
82+
- `__FILE__`: Current filename.
83+
- `__LINE__`: Current line number.
84+
85+
### Example
86+
87+
The following example uses the above predefined macros:
88+
89+
```c
90+
#include <stdio.h>
91+
int main() {
92+
93+
char file[] = __FILE__;
94+
char date[] = __DATE__;
95+
char time[] = __TIME__;
96+
int line = __LINE__;
97+
98+
printf("File name: %s\n", file);
99+
printf("Date: %s\n", date);
100+
printf("Time: %s\n", time);
101+
printf("Line number: %d\n", line);
102+
}
103+
```
104+
105+
The output resembles the following:
106+
107+
```shell
108+
File name: main.c
109+
Date: Jun 25 2022
110+
Time: 14:05:33
111+
Line number: 7
112+
```
113+
114+
## Undefining a Macro
115+
116+
Once defined, a macro can be undefined with the `#undef` command. Using the macro after that point will result in a compile error.
117+
118+
### Example
119+
120+
```c
121+
#include <stdio.h>
122+
#define TEST 1
123+
124+
int main() {
125+
#ifdef TEST
126+
printf("TEST defined\n");
127+
#else
128+
printf("TEST undefined\n");
129+
#endif
130+
131+
#undef TEST
132+
133+
#ifdef TEST
134+
printf("TEST defined\n");
135+
#else
136+
printf("TEST undefined\n");
137+
#endif
138+
139+
}
140+
```
141+
142+
This results in the output:
143+
144+
```shell
145+
TEST defined
146+
TEST undefined
147+
```

content/c/concepts/preprocessors/preprocessors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ The example above will compile different code for different operating systems ba
6363

6464
### Defining Macros
6565

66-
When the preprocessor encounters a macro in the source code, it will replace the macro with the value it has been assigned. Macros are defined by using `#define` and can be undefined using `#undef`.
66+
When the preprocessor encounters a [macro](https://www.codecademy.com/resources/docs/c/macros) in the source code, it will replace the macro with the value it has been assigned. Macros are defined by using `#define` and can be undefined using `#undef`.
6767

6868
```c
6969
#define PI 3.1416
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
Title: 'Macros'
3+
Description: 'A macro is a label defined in the source code that is replaced by its value by the preprocessor before compilation.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Command Line'
9+
- 'Developer Tools'
10+
CatalogContent:
11+
- 'learn-c-plus-plus'
12+
- 'paths/computer-science'
13+
---
14+
15+
A **macro** is a label defined in the source code that is replaced by its value by the preprocessor before compilation. Macros are initialized with the `#define` preprocessor command and can be undefined with the `#undef` command.
16+
17+
There are two types of macros: [object](https://www.codecademy.com/resources/docs/cpp/objects)-like macros and [function](https://www.codecademy.com/resources/docs/cpp/functions)-like macros.
18+
19+
## Object-Like Macros
20+
21+
These macros are replaced by their value in the source code before compilation. Their primary purpose is to define constants to be used in the code.
22+
23+
**Note**: Macro definitions are not followed by a semicolon `;`.
24+
25+
### Example
26+
27+
In the following example, `PI` is defined as an object-like macro:
28+
29+
```cpp
30+
#include <iostream>
31+
using namespace std;
32+
33+
#define PI 3.1416
34+
35+
int main() {
36+
float radius = 3;
37+
float area;
38+
area = PI * radius * radius;
39+
cout << "Area is " << area;
40+
return 0;
41+
}
42+
```
43+
44+
This example outputs the following:
45+
46+
```shell
47+
Area is 28.2744
48+
```
49+
50+
## Function-Like Macros
51+
52+
These macros behave like functions, in that they take arguments that are used in the replaced code.
53+
54+
**Note**: When defining a function-like macro, there cannot be a space between the macro name and the opening parenthesis.
55+
56+
### Example
57+
58+
In the following example, `AREA` is defined as a function-like macro. Note that other macros can be used in defining a subsequent macro. The inner macro is replaced by its value before the outer macro is replaced.
59+
60+
```cpp
61+
#include <iostream>
62+
using namespace std;
63+
64+
#define PI 3.1416
65+
#define AREA(r) r * r * PI
66+
67+
int main() {
68+
float radius = 5;
69+
float result;
70+
result = AREA(radius);
71+
cout << "Area is " << result;
72+
return 0;
73+
}
74+
```
75+
76+
This example outputs the following:
77+
78+
```shell
79+
Area is 78.54
80+
```
81+
82+
## Predefined Macros
83+
84+
C++ has a number of predefined macros, including the following:
85+
86+
- `__DATE__`: Current date formatted as `MMM DD YYYY`.
87+
- `__TIME__`: Current time formatted as `HH:MM:SS`.
88+
- `__FILE__`: Current filename.
89+
- `__LINE__`: Current line number.
90+
91+
### Example
92+
93+
The following example uses the predefined macros mentioned above:
94+
95+
```cpp
96+
#include <iostream>
97+
using namespace std;
98+
99+
int main() {
100+
101+
char file[] = __FILE__;
102+
char date[] = __DATE__;
103+
char time[] = __TIME__;
104+
int line = __LINE__;
105+
106+
cout << "File name: " << file << "\n";
107+
cout << "Date: " << date << "\n";
108+
cout << "Time: " << time << "\n";
109+
cout << "Line number: " << line << "\n";
110+
}
111+
```
112+
113+
The output resembles the following:
114+
115+
```shell
116+
File name: main.cpp
117+
Date: Jun 25 2022
118+
Time: 14:17:50
119+
Line number: 9
120+
```
121+
122+
## Undefining a Macro
123+
124+
Once defined, a macro can be undefined with the `#undef` command. Using the macro after that point will result in a compile error.
125+
126+
### Example
127+
128+
```cpp
129+
#include <iostream>
130+
using namespace std;
131+
132+
#define TEST 1
133+
134+
int main() {
135+
#ifdef TEST
136+
cout << "TEST defined\n";
137+
#else
138+
cout << "TEST undefined\n";
139+
#endif
140+
141+
#undef TEST
142+
143+
#ifdef TEST
144+
cout << "TEST defined\n";
145+
#else
146+
cout << "TEST undefined\n";
147+
#endif
148+
149+
}
150+
```
151+
152+
This results in the output:
153+
154+
```shell
155+
TEST defined
156+
TEST undefined
157+
```

0 commit comments

Comments
 (0)