Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Core Directives | Conditional compilation
C Preprocessing

Свайпніть щоб показати меню

book
Core Directives

c

main

copy
12345678910
#include <stdio.h> #define VERSION 2 int main() { //If VERSION equals 2, this block will be compiled #if VERSION == 2 printf("Version 2 is active\n"); #endif return 0; }

Use it to include or exclude a code block based on a compile-time constan and to create multiple versions of a program depending on platform, version, or configuration.

It's useful for eliminates code from the compiled binary entirely, for portable code, for different behavior for Windows/Linux, for helps enable/disable debugging or logging code.

It's useful because it keeps the preprocessor logic valid and allows for clear structure and grouping.

Use it when you have started a conditional compilation block — it's mandatory to close it.

c

main

copy
123456789101112
#include <stdio.h> #define VERSION 1 int main() { #if VERSION == 2 printf("Version 2 is active\n"); #elif VERSION == 1 printf("Version 1 is active\n"); #endif return 0; }

Use it when you have multiple exclusive compile-time conditions and need to select one, or when you want to avoid nested #if...#else blocks.

It's useful for keeping configuration logic clean and readable, and for avoiding deeply nested structures with multiple #if directives.

The #else directive handles the case when none of the previous conditions (#if, #elif) were true.

c

main

copy
1234567891011121314
#include <stdio.h> #define VERSION 5 int main() { #if VERSION == 1 printf("Version 1\n"); #elif VERSION == 2 printf("Version 2\n"); #else printf("Unknown version\n"); #endif return 0; }

Use it when you want to define a default code path if all previous #if or #elif conditions fail, and to ensure that only one block of code gets compiled.

It's useful because it guarantees that fallbacks are defined and makes it easier to handle cases such as when debugging is disabled.

Завдання

Swipe to start coding

You are developing a program with logging for different levels (error, warning, information), and you need to choose which logs to include at compile time.

  • Use the #if directive to write conditions for handling each logging level, namely LOG_ERROR, LOG_WARNING, LOG_INFO.

Рішення

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

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

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

Секція 3. Розділ 2

Запитати АІ

expand
ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

book
Core Directives

c

main

copy
12345678910
#include <stdio.h> #define VERSION 2 int main() { //If VERSION equals 2, this block will be compiled #if VERSION == 2 printf("Version 2 is active\n"); #endif return 0; }

Use it to include or exclude a code block based on a compile-time constan and to create multiple versions of a program depending on platform, version, or configuration.

It's useful for eliminates code from the compiled binary entirely, for portable code, for different behavior for Windows/Linux, for helps enable/disable debugging or logging code.

It's useful because it keeps the preprocessor logic valid and allows for clear structure and grouping.

Use it when you have started a conditional compilation block — it's mandatory to close it.

c

main

copy
123456789101112
#include <stdio.h> #define VERSION 1 int main() { #if VERSION == 2 printf("Version 2 is active\n"); #elif VERSION == 1 printf("Version 1 is active\n"); #endif return 0; }

Use it when you have multiple exclusive compile-time conditions and need to select one, or when you want to avoid nested #if...#else blocks.

It's useful for keeping configuration logic clean and readable, and for avoiding deeply nested structures with multiple #if directives.

The #else directive handles the case when none of the previous conditions (#if, #elif) were true.

c

main

copy
1234567891011121314
#include <stdio.h> #define VERSION 5 int main() { #if VERSION == 1 printf("Version 1\n"); #elif VERSION == 2 printf("Version 2\n"); #else printf("Unknown version\n"); #endif return 0; }

Use it when you want to define a default code path if all previous #if or #elif conditions fail, and to ensure that only one block of code gets compiled.

It's useful because it guarantees that fallbacks are defined and makes it easier to handle cases such as when debugging is disabled.

Завдання

Swipe to start coding

You are developing a program with logging for different levels (error, warning, information), and you need to choose which logs to include at compile time.

  • Use the #if directive to write conditions for handling each logging level, namely LOG_ERROR, LOG_WARNING, LOG_INFO.

Рішення

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

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

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

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