-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New Entry C & C++ Macros #824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
6d9b894
Create macros.md
SSwiniarski 1273aa0
Merge branch 'Codecademy:main' into macros
SSwiniarski 52b3317
Update preprocessors.md
SSwiniarski edce106
Update macros.md
SSwiniarski 5bd44d1
Update macros.md
SSwiniarski 24447cd
Create macros.md
SSwiniarski 6c98620
yarn format
SSwiniarski 74cf54d
Update content/c/concepts/macros/macros.md
SSwiniarski 0d54fb5
Update content/c/concepts/macros/macros.md
SSwiniarski 2352d5e
Update content/c/concepts/macros/macros.md
SSwiniarski 6bd96e9
Update content/c/concepts/macros/macros.md
SSwiniarski f339de2
Update content/c/concepts/macros/macros.md
SSwiniarski 5361b75
Update content/cpp/concepts/macros/macros.md
SSwiniarski a42e895
Update content/c/concepts/macros/macros.md
SSwiniarski 491dd7f
Update content/c/concepts/macros/macros.md
SSwiniarski bab55cc
Update content/cpp/concepts/macros/macros.md
SSwiniarski b558c1d
Update content/c/concepts/macros/macros.md
SSwiniarski 78c8679
Update content/cpp/concepts/macros/macros.md
SSwiniarski 0d7efc1
Update content/cpp/concepts/macros/macros.md
SSwiniarski cf79e68
Update content/cpp/concepts/macros/macros.md
SSwiniarski 759df64
Update content/cpp/concepts/macros/macros.md
SSwiniarski 1f00541
Update content/cpp/concepts/macros/macros.md
SSwiniarski e2a0fe2
Update content/cpp/concepts/macros/macros.md
SSwiniarski e937c15
Update content/cpp/concepts/macros/macros.md
SSwiniarski 3ae6480
Update content/cpp/concepts/macros/macros.md
SSwiniarski 47162b9
Update content/c/concepts/macros/macros.md
SSwiniarski 0d93ba3
Update content/cpp/concepts/macros/macros.md
SSwiniarski 6e66fa7
Update macros.md
SSwiniarski 05e2f92
Update macros.md
SSwiniarski 03186ea
yarn format
SSwiniarski 1ec295f
Merge branch 'main' into macros
SSwiniarski ce76f63
Update macros.md
Dusch4593 8371cbf
Update macros.md
Dusch4593 14416d1
Merge branch 'main' into macros
Dusch4593 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ``` | ||
SSwiniarski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## 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 | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.