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

bookStringify Operators

Stringify operator #

The # operator is used to convert a macro argument into a string literal. This means that if you pass any value to the macro, it will be converted to a string.

Here is an example of usage:

main.c

main.c

copy
123456789
#include <stdio.h> #define SQR(x) printf("Square " #x " = %d\n", ((x)*(x))) int main() { int j = 5; SQR(j); return 0; }

The #x operator in the SQR macro is used to turn the x argument into a string. When you pass the j variable to the macro, #x produces the string "j" (i.e. the variable name), not its value.

Thus, in the line

printf("Square " #x " = %d\n", )(x)*(x)));

#x causes the variable name to be included in the output, and x passes its value (in this case, 5).

If you use a number as an argument, #x will turn the number into a string and then treat it as a number:

main.c

main.c

copy
12345678910
#include <stdio.h> #define SQR(x) printf("Square " #x " = %d\n", ((x)*(x))) int main() { int j = 5; SQR(j); SQR (54); // number as argument return 0; }

Token Pasting operator ##

The ## operation combines two tokens into one.

main.c

main.c

copy
12345678
#include <stdio.h> #define CONCAT(x, y) x##y int main() { int myVariable = 10; // variable named `myVariable` printf("%d\n", CONCAT(my, Variable)); // Result: 10 return 0; }

The CONCAT(x, y) macro concatenates the tokens my and Variable, which results in the name myVariable - just like a variable name.

When you call CONCAT(x, y), the preprocessor replaces it with myVariable, and you can access the value of myVariable.

Завдання

Swipe to start coding

  1. Complete the CREATE_VAR macro, which will concatenate the tokens token1 and token2;
  2. Use the created macro to create the variable "myVariable";
  3. Specify the name of the variable whose contents you want to display on the screen.

Рішення

Все було зрозуміло?

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

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

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

single

Запитати АІ

expand

Запитати АІ

ChatGPT

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

close

Awesome!

Completion rate improved to 5.56

bookStringify Operators

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

Stringify operator #

The # operator is used to convert a macro argument into a string literal. This means that if you pass any value to the macro, it will be converted to a string.

Here is an example of usage:

main.c

main.c

copy
123456789
#include <stdio.h> #define SQR(x) printf("Square " #x " = %d\n", ((x)*(x))) int main() { int j = 5; SQR(j); return 0; }

The #x operator in the SQR macro is used to turn the x argument into a string. When you pass the j variable to the macro, #x produces the string "j" (i.e. the variable name), not its value.

Thus, in the line

printf("Square " #x " = %d\n", )(x)*(x)));

#x causes the variable name to be included in the output, and x passes its value (in this case, 5).

If you use a number as an argument, #x will turn the number into a string and then treat it as a number:

main.c

main.c

copy
12345678910
#include <stdio.h> #define SQR(x) printf("Square " #x " = %d\n", ((x)*(x))) int main() { int j = 5; SQR(j); SQR (54); // number as argument return 0; }

Token Pasting operator ##

The ## operation combines two tokens into one.

main.c

main.c

copy
12345678
#include <stdio.h> #define CONCAT(x, y) x##y int main() { int myVariable = 10; // variable named `myVariable` printf("%d\n", CONCAT(my, Variable)); // Result: 10 return 0; }

The CONCAT(x, y) macro concatenates the tokens my and Variable, which results in the name myVariable - just like a variable name.

When you call CONCAT(x, y), the preprocessor replaces it with myVariable, and you can access the value of myVariable.

Завдання

Swipe to start coding

  1. Complete the CREATE_VAR macro, which will concatenate the tokens token1 and token2;
  2. Use the created macro to create the variable "myVariable";
  3. Specify the name of the variable whose contents you want to display on the screen.

Рішення

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

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

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

close

Awesome!

Completion rate improved to 5.56
Секція 2. Розділ 5
single

single

some-alt