Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Advanced Usage of Symbolic Constant | Macros
C Preprocessing
course content

Conteúdo do Curso

C Preprocessing

C Preprocessing

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

book
Advanced Usage of Symbolic Constant

Let's consider practical examples of using macro substitutions.

Multi-Line Macro

The \ (backslash) character in macros is used to continue the macro definition to the next line.

c

main

copy
1234567891011
#include <stdio.h> #define PRINT_SUM(a, b) \ int sum = (a) + (b); \ printf("Sum: %d\n", sum); int main() { PRINT_SUM(3, 5); // Call a macro that prints "Sum: 8" return 0; }

Code duplication

This code uses the #define macro to shorten and avoid duplication of the same fields in two structures (struct Product and struct Customer)

c

main

copy
1234567891011121314151617181920212223242526272829
#include <stdio.h> #define COMMON_FIELDS \ int id; \ char name[50]; \ float price; // Declare the Product structure struct Product { COMMON_FIELDS // Using the macro char category[30]; }; // Declare the Customer structure struct Customer { COMMON_FIELDS // Using the macro char email[50]; char address[100]; }; int main() { struct Product p1 = {1, "Laptop", 999.99, "Electronics"}; struct Customer c1 = {2, "Alice", 0.0, "alice@example.com", "123 Main St"}; printf("Product: %s,\n Category: %s,\n Price: %.2f\n", p1.name, p1.category, p1.price); printf("Customer: %s,\n Email: %s,\n Address: %s\n", c1.name, c1.email, c1.address); return 0; }

Checking Logical Conditions

The macro automatically replaces the validation code without the need to write it manually and allows you to display useful information about the error.

c

main

copy
123456789101112
#include <stdio.h> #define CHECK(condition) \ if (!(condition)) { \ printf("Error: %s not running!\n", #condition); \ } int main() { int x = 5; CHECK(x > 0); // Executes without error CHECK(x == 10); // Print an error message return 0; }

Logging

The LOG_LEVEL macro sets the logging level (LOG_WARNING). Only ERROR and WARNING level messages will be output, and INFO will be ignored.

c

main

copy
1234567891011121314151617181920212223242526272829303132
#include <stdio.h> // Defining logging levels #define LOG_ERROR 1 // For critical errors #define LOG_WARNING 2 // For reporting potential problems #define LOG_INFO 3 // For general information about program execution #define LOG_LEVEL LOG_WARNING // Set the desired logging level void log_message(int level, const char *message) { if (level <= LOG_LEVEL) { // Condition for checking the logging level switch (level) { case LOG_ERROR: printf("ERROR: %s\n", message); break; case LOG_WARNING: printf("WARNING: %s\n", message); break; case LOG_INFO: printf("INFO: %s\n", message); break; default: break; } } } int main() { log_message(LOG_ERROR, "This is an error message"); log_message(LOG_WARNING, "This is a warning message"); log_message(LOG_INFO, "This is an info message"); return 0; }
Tarefa

Swipe to start coding

Add a new logging level.

  1. Set the logging level for LOG_DEBUG;
  2. Set the desired logging level;
  3. Set a condition that checks whether a message should be output for a specific logging level;
  4. Specify the message text that will be displayed if the condition is not met.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 3
toggle bottom row

book
Advanced Usage of Symbolic Constant

Let's consider practical examples of using macro substitutions.

Multi-Line Macro

The \ (backslash) character in macros is used to continue the macro definition to the next line.

c

main

copy
1234567891011
#include <stdio.h> #define PRINT_SUM(a, b) \ int sum = (a) + (b); \ printf("Sum: %d\n", sum); int main() { PRINT_SUM(3, 5); // Call a macro that prints "Sum: 8" return 0; }

Code duplication

This code uses the #define macro to shorten and avoid duplication of the same fields in two structures (struct Product and struct Customer)

c

main

copy
1234567891011121314151617181920212223242526272829
#include <stdio.h> #define COMMON_FIELDS \ int id; \ char name[50]; \ float price; // Declare the Product structure struct Product { COMMON_FIELDS // Using the macro char category[30]; }; // Declare the Customer structure struct Customer { COMMON_FIELDS // Using the macro char email[50]; char address[100]; }; int main() { struct Product p1 = {1, "Laptop", 999.99, "Electronics"}; struct Customer c1 = {2, "Alice", 0.0, "alice@example.com", "123 Main St"}; printf("Product: %s,\n Category: %s,\n Price: %.2f\n", p1.name, p1.category, p1.price); printf("Customer: %s,\n Email: %s,\n Address: %s\n", c1.name, c1.email, c1.address); return 0; }

Checking Logical Conditions

The macro automatically replaces the validation code without the need to write it manually and allows you to display useful information about the error.

c

main

copy
123456789101112
#include <stdio.h> #define CHECK(condition) \ if (!(condition)) { \ printf("Error: %s not running!\n", #condition); \ } int main() { int x = 5; CHECK(x > 0); // Executes without error CHECK(x == 10); // Print an error message return 0; }

Logging

The LOG_LEVEL macro sets the logging level (LOG_WARNING). Only ERROR and WARNING level messages will be output, and INFO will be ignored.

c

main

copy
1234567891011121314151617181920212223242526272829303132
#include <stdio.h> // Defining logging levels #define LOG_ERROR 1 // For critical errors #define LOG_WARNING 2 // For reporting potential problems #define LOG_INFO 3 // For general information about program execution #define LOG_LEVEL LOG_WARNING // Set the desired logging level void log_message(int level, const char *message) { if (level <= LOG_LEVEL) { // Condition for checking the logging level switch (level) { case LOG_ERROR: printf("ERROR: %s\n", message); break; case LOG_WARNING: printf("WARNING: %s\n", message); break; case LOG_INFO: printf("INFO: %s\n", message); break; default: break; } } } int main() { log_message(LOG_ERROR, "This is an error message"); log_message(LOG_WARNING, "This is a warning message"); log_message(LOG_INFO, "This is an info message"); return 0; }
Tarefa

Swipe to start coding

Add a new logging level.

  1. Set the logging level for LOG_DEBUG;
  2. Set the desired logging level;
  3. Set a condition that checks whether a message should be output for a specific logging level;
  4. Specify the message text that will be displayed if the condition is not met.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 3
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Sentimos muito que algo saiu errado. O que aconteceu?
some-alt