From 84071736e2964f06fc657a40b70c3131c379df7d Mon Sep 17 00:00:00 2001 From: pro100ton Date: Thu, 23 Jan 2025 22:48:21 +0300 Subject: [PATCH] Going thorugh preprocessor chapter --- c_modern_approach/Preprocessor/celcius.c | 17 +++++++++++++++ .../Preprocessor/double_hashtag_op.c | 7 +++++++ c_modern_approach/Preprocessor/macro_rules.c | 21 +++++++++++++++++++ .../Preprocessor/parametrized_macroses.c | 9 ++++++++ c_modern_approach/Preprocessor/playground.c | 9 ++++++++ c_modern_approach/Preprocessor/undef.c | 10 +++++++++ c_modern_approach/README.md | 2 ++ 7 files changed, 75 insertions(+) create mode 100644 c_modern_approach/Preprocessor/celcius.c create mode 100644 c_modern_approach/Preprocessor/double_hashtag_op.c create mode 100644 c_modern_approach/Preprocessor/macro_rules.c create mode 100644 c_modern_approach/Preprocessor/parametrized_macroses.c create mode 100644 c_modern_approach/Preprocessor/playground.c create mode 100644 c_modern_approach/Preprocessor/undef.c create mode 100644 c_modern_approach/README.md diff --git a/c_modern_approach/Preprocessor/celcius.c b/c_modern_approach/Preprocessor/celcius.c new file mode 100644 index 0000000..9f1678e --- /dev/null +++ b/c_modern_approach/Preprocessor/celcius.c @@ -0,0 +1,17 @@ +#include "stdio.h" +#include + +#define FREEZING_PT 32.0f +#define SCALE_FACTOR (5.0f / 9.0f) + +int main(void) { + float farenheit, celsius; + + printf("Enter Fahrenheit temperature: "); + scanf("%f", &farenheit); + + celsius = (farenheit - FREEZING_PT) * SCALE_FACTOR; + + printf("Celcius equivalent is: %.1f\n", celsius); + return 0; +} diff --git a/c_modern_approach/Preprocessor/double_hashtag_op.c b/c_modern_approach/Preprocessor/double_hashtag_op.c new file mode 100644 index 0000000..08dcae9 --- /dev/null +++ b/c_modern_approach/Preprocessor/double_hashtag_op.c @@ -0,0 +1,7 @@ +#include +#define MK_ID(n) i##n + +int main(int argc, char *argv[]) { + int MK_ID(1); // This converts to `int i1` + printf("%d", i1); +} diff --git a/c_modern_approach/Preprocessor/macro_rules.c b/c_modern_approach/Preprocessor/macro_rules.c new file mode 100644 index 0000000..600f2af --- /dev/null +++ b/c_modern_approach/Preprocessor/macro_rules.c @@ -0,0 +1,21 @@ +#include +#define SIZE 256 + +int main(int argc, char *argv[]) +{ + int BUFFER_SIZE; + if (BUFFER_SIZE > SIZE) + puts("ERROR: SIZE exceeded"); +} + +/* + * Код выше трансформируется в следующее: + * + * + * int main(int argc, char *argv[]) + * { + * int BUFFER_SIZE; + * if (BUFFER_SIZE > 256) + * puts("ERROR: SIZE exceeded"); + * } +*/ diff --git a/c_modern_approach/Preprocessor/parametrized_macroses.c b/c_modern_approach/Preprocessor/parametrized_macroses.c new file mode 100644 index 0000000..c4905bf --- /dev/null +++ b/c_modern_approach/Preprocessor/parametrized_macroses.c @@ -0,0 +1,9 @@ +#include +#define MAX(x, y) ((x) > (y) ? (x) : (y)) + +int main(int argc, char *argv[]) { + int j, k; + j = 10; + k = 20; + printf("%d", MAX(j, k)); +} diff --git a/c_modern_approach/Preprocessor/playground.c b/c_modern_approach/Preprocessor/playground.c new file mode 100644 index 0000000..048d3aa --- /dev/null +++ b/c_modern_approach/Preprocessor/playground.c @@ -0,0 +1,9 @@ +#include +#define PRINT_INT(n) printf(#n " AND " #n " = %d\n", n) + +int main(void) { + int i, j; + i = 10; + j = 20; + PRINT_INT(i / j); +} diff --git a/c_modern_approach/Preprocessor/undef.c b/c_modern_approach/Preprocessor/undef.c new file mode 100644 index 0000000..dc6cf66 --- /dev/null +++ b/c_modern_approach/Preprocessor/undef.c @@ -0,0 +1,10 @@ +#include +#define SIZE 100 +int main(int argc, char *argv[]) { + int i; + i = 101; +#undef SIZE + if (i > SIZE) { + printf("Yey!\n"); + } +} diff --git a/c_modern_approach/README.md b/c_modern_approach/README.md new file mode 100644 index 0000000..f82190f --- /dev/null +++ b/c_modern_approach/README.md @@ -0,0 +1,2 @@ +# Точка останова +Creating longer macros - 328