Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Symbolic Constant | Macros
C Preprocessing
course content

Зміст курсу

C Preprocessing

C Preprocessing

1. Introduction to Preprocessing
2. Macros
3. Conditional compilation

book
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:

c

main

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.
Завдання

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.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 1
toggle bottom row

book
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:

c

main

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.
Завдання

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.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 1
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Ми дуже хвилюємося, що щось пішло не так. Що трапилося?
some-alt