Symbolic Constant
Each #define line consists of three parts:
The body of a macro can be not only a single numeric value, but also other macros or entire expressions:
main.c
12345678910#include <stdio.h> #define TWO 2 // macros with value. #define RESULT TWO*TWO // TWO is replaced by the number 2. #define PRINT printf("result = %d", RESULT); int main() { PRINT return 0; }
Every time the compiler encounters βTWOβ, it will simply substitute its value 2
.
The macro is immutable. Once defined, it is a permanent textual substitution within the file or scope where it is defined.
Why do we need this? Why not just use variables?
What is better to use?:
- If the value never changes, const is better, because it is safer;
- If you need to substitute a simple number or text, you can use a macro.
- Create an object macro
add
, the body of the macro is an addition operator+
. - Use a macro to add any two numbers.
Swipe to start coding
- Create an object macro
add
, the body of the macro is an addition operator+
; - Use a macro to add any two numbers.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Summarize this chapter
Explain the code in file
Explain why file doesn't solve the task
Awesome!
Completion rate improved to 5.56
Symbolic Constant
Swipe to show menu
Each #define line consists of three parts:
The body of a macro can be not only a single numeric value, but also other macros or entire expressions:
main.c
12345678910#include <stdio.h> #define TWO 2 // macros with value. #define RESULT TWO*TWO // TWO is replaced by the number 2. #define PRINT printf("result = %d", RESULT); int main() { PRINT return 0; }
Every time the compiler encounters βTWOβ, it will simply substitute its value 2
.
The macro is immutable. Once defined, it is a permanent textual substitution within the file or scope where it is defined.
Why do we need this? Why not just use variables?
What is better to use?:
- If the value never changes, const is better, because it is safer;
- If you need to substitute a simple number or text, you can use a macro.
- Create an object macro
add
, the body of the macro is an addition operator+
. - Use a macro to add any two numbers.
Swipe to start coding
- Create an object macro
add
, the body of the macro is an addition operator+
; - Use a macro to add any two numbers.
Solution
Thanks for your feedback!
Awesome!
Completion rate improved to 5.56single