Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Symbolic Constant | Macros
C Preprocessing

bookSymbolic 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

main.c

copy
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.
  1. Create an object macro add, the body of the macro is an addition operator +.
  2. Use a macro to add any two numbers.
Task

Swipe to start coding

  1. Create an object macro add, the body of the macro is an addition operator +;
  2. Use a macro to add any two numbers.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you show me how to define the macro in C?

How do I use the macro to add two numbers?

Can you explain what an object macro is?

close

Awesome!

Completion rate improved to 5.56

bookSymbolic 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

main.c

copy
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.
  1. Create an object macro add, the body of the macro is an addition operator +.
  2. Use a macro to add any two numbers.
Task

Swipe to start coding

  1. Create an object macro add, the body of the macro is an addition operator +;
  2. Use a macro to add any two numbers.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
single

single

some-alt