Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
6d9b894
Create macros.md
SSwiniarski Jun 25, 2022
1273aa0
Merge branch 'Codecademy:main' into macros
SSwiniarski Jun 25, 2022
52b3317
Update preprocessors.md
SSwiniarski Jun 25, 2022
edce106
Update macros.md
SSwiniarski Jun 25, 2022
5bd44d1
Update macros.md
SSwiniarski Jun 25, 2022
24447cd
Create macros.md
SSwiniarski Jun 25, 2022
6c98620
yarn format
SSwiniarski Jun 25, 2022
74cf54d
Update content/c/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
0d54fb5
Update content/c/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
2352d5e
Update content/c/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
6bd96e9
Update content/c/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
f339de2
Update content/c/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
5361b75
Update content/cpp/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
a42e895
Update content/c/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
491dd7f
Update content/c/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
bab55cc
Update content/cpp/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
b558c1d
Update content/c/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
78c8679
Update content/cpp/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
0d7efc1
Update content/cpp/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
cf79e68
Update content/cpp/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
759df64
Update content/cpp/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
1f00541
Update content/cpp/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
e2a0fe2
Update content/cpp/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
e937c15
Update content/cpp/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
3ae6480
Update content/cpp/concepts/macros/macros.md
SSwiniarski Jun 25, 2022
47162b9
Update content/c/concepts/macros/macros.md
SSwiniarski Jun 29, 2022
0d93ba3
Update content/cpp/concepts/macros/macros.md
SSwiniarski Jun 29, 2022
6e66fa7
Update macros.md
SSwiniarski Jun 29, 2022
05e2f92
Update macros.md
SSwiniarski Jun 29, 2022
03186ea
yarn format
SSwiniarski Jun 29, 2022
1ec295f
Merge branch 'main' into macros
SSwiniarski Jun 29, 2022
ce76f63
Update macros.md
Dusch4593 Jun 30, 2022
8371cbf
Update macros.md
Dusch4593 Jun 30, 2022
14416d1
Merge branch 'main' into macros
Dusch4593 Jun 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions content/c/concepts/macros/macros.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
---
Title: 'Macros'
Description: 'A macro is a label defined in the source code that is replaced by its value by the preprocessor before compilation.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Command Line'
- 'Developer Tools'
CatalogContent:
- 'learn-c'
- 'paths/computer-science'
---

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.

There are two types of macros: object-like macros and function-like macros.

## Object-Like Macros

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.

**Note**: Macro definitions are not followed by a semicolon `;`.

### Example

In the following example, `PI` is defined as an object-like macro:

```c
#include <stdio.h>
#define PI 3.1416

int main() {
float radius = 3;
float area;
area = PI * radius * radius;
printf("Area is: %f",area);
return 0;
}
```

This example outputs the following:

```shell
Area is: 28.274401
```

## Function-Like Macros

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.

### Example

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.

```c
#include <stdio.h>
#define PI 3.1416
#define AREA(r) r * r * PI

int main() {
float radius = 5;
float result;
result = AREA(radius);
printf("Area is: %f",result);
return 0;
}
```

This example outputs the following:

```shell
Area is: 78.540001
```

## Predefined Macros

C has a number of predefined macros, including the following:

- `__DATE__`: Current date formatted as `MMM DD YYYY`.
- `__TIME__`: Current time formatted as `HH:MM:SS`.
- `__FILE__`: Current filename.
- `__LINE__`: Current line number.

### Example

The following example uses the above predefined macros:

```c
#include <stdio.h>
int main() {

char file[] = __FILE__;
char date[] = __DATE__;
char time[] = __TIME__;
int line = __LINE__;

printf("File name: %s\n", file);
printf("Date: %s\n", date);
printf("Time: %s\n", time);
printf("Line number: %d\n", line);
}
```

The output resembles the following:

```shell
File name: main.c
Date: Jun 25 2022
Time: 14:05:33
Line number: 7
```

## Undefining a Macro

Once defined, a macro can be undefined with the `#undef` command. Using the macro after that point will result in a compile error.

### Example

```c
#include <stdio.h>
#define TEST 1

int main() {
#ifdef TEST
printf("TEST defined\n");
#else
printf("TEST undefined\n");
#endif

#undef TEST

#ifdef TEST
printf("TEST defined\n");
#else
printf("TEST undefined\n");
#endif

}
```

This results in the output:

```shell
TEST defined
TEST undefined
```
2 changes: 1 addition & 1 deletion content/c/concepts/preprocessors/preprocessors.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ The example above will compile different code for different operating systems ba

### Defining Macros

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`.
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`.

```c
#define PI 3.1416
Expand Down
157 changes: 157 additions & 0 deletions content/cpp/concepts/macros/macros.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
---
Title: 'Macros'
Description: 'A macro is a label defined in the source code that is replaced by its value by the preprocessor before compilation.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Command Line'
- 'Developer Tools'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

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.

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.

## Object-Like Macros

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.

**Note**: Macro definitions are not followed by a semicolon `;`.

### Example

In the following example, `PI` is defined as an object-like macro:

```cpp
#include <iostream>
using namespace std;

#define PI 3.1416

int main() {
float radius = 3;
float area;
area = PI * radius * radius;
cout << "Area is " << area;
return 0;
}
```

This example outputs the following:

```shell
Area is 28.2744
```

## Function-Like Macros

These macros behave like functions, in that they take arguments that are used in the replaced code.

**Note**: When defining a function-like macro, there cannot be a space between the macro name and the opening parenthesis.

### Example

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.

```cpp
#include <iostream>
using namespace std;

#define PI 3.1416
#define AREA(r) r * r * PI

int main() {
float radius = 5;
float result;
result = AREA(radius);
cout << "Area is " << result;
return 0;
}
```

This example outputs the following:

```shell
Area is 78.54
```

## Predefined Macros

C++ has a number of predefined macros, including the following:

- `__DATE__`: Current date formatted as `MMM DD YYYY`.
- `__TIME__`: Current time formatted as `HH:MM:SS`.
- `__FILE__`: Current filename.
- `__LINE__`: Current line number.

### Example

The following example uses the predefined macros mentioned above:

```cpp
#include <iostream>
using namespace std;

int main() {

char file[] = __FILE__;
char date[] = __DATE__;
char time[] = __TIME__;
int line = __LINE__;

cout << "File name: " << file << "\n";
cout << "Date: " << date << "\n";
cout << "Time: " << time << "\n";
cout << "Line number: " << line << "\n";
}
```

The output resembles the following:

```shell
File name: main.cpp
Date: Jun 25 2022
Time: 14:17:50
Line number: 9
```

## Undefining a Macro

Once defined, a macro can be undefined with the `#undef` command. Using the macro after that point will result in a compile error.

### Example

```cpp
#include <iostream>
using namespace std;

#define TEST 1

int main() {
#ifdef TEST
cout << "TEST defined\n";
#else
cout << "TEST undefined\n";
#endif

#undef TEST

#ifdef TEST
cout << "TEST defined\n";
#else
cout << "TEST undefined\n";
#endif

}
```

This results in the output:

```shell
TEST defined
TEST undefined
```