diff --git a/c_modern_approach/Preprocessor/celcius.c b/c_modern_approach/Preprocessor/celcius.c index 9f1678e..31b8532 100644 --- a/c_modern_approach/Preprocessor/celcius.c +++ b/c_modern_approach/Preprocessor/celcius.c @@ -2,14 +2,17 @@ #include #define FREEZING_PT 32.0f -#define SCALE_FACTOR (5.0f / 9.0f) +#define BEGIN { +#define END } -int main(void) { +int main(void) +BEGIN float farenheit, celsius; printf("Enter Fahrenheit temperature: "); scanf("%f", &farenheit); + #define SCALE_FACTOR (5.0f / 9.0f) celsius = (farenheit - FREEZING_PT) * SCALE_FACTOR; printf("Celcius equivalent is: %.1f\n", celsius); diff --git a/c_modern_approach/Preprocessor/double_hashtag_op.c b/c_modern_approach/Preprocessor/double_hashtag_op.c index 08dcae9..9cba086 100644 --- a/c_modern_approach/Preprocessor/double_hashtag_op.c +++ b/c_modern_approach/Preprocessor/double_hashtag_op.c @@ -1,7 +1,15 @@ #include -#define MK_ID(n) i##n +#define MK_ID(n) kakulya_##n + +#define GENERIC_MAX(type) \ + type type##_max(type x, type y) { return x > y ? x : y; } + +GENERIC_MAX(float); +/* The macro above will transorf into something like following: + * float float_max(float x, float y) { return x > y ? x : y; } + */ int main(int argc, char *argv[]) { - int MK_ID(1); // This converts to `int i1` - printf("%d", i1); + int MK_ID(1); // This converts to `int kakulya_1` + printf("%d", kakulya_1); } diff --git a/c_modern_approach/Preprocessor/parametrized_macroses.c b/c_modern_approach/Preprocessor/parametrized_macroses.c index c4905bf..78f3bba 100644 --- a/c_modern_approach/Preprocessor/parametrized_macroses.c +++ b/c_modern_approach/Preprocessor/parametrized_macroses.c @@ -1,9 +1,13 @@ #include #define MAX(x, y) ((x) > (y) ? (x) : (y)) +#define IS_EVEN(n) ((n) % 2 == 0) +#define IS_EVEN_STR(n) ((n) % 2 == 0 ? "yes" : "no") int main(int argc, char *argv[]) { int j, k; j = 10; k = 20; - printf("%d", MAX(j, k)); + printf("%d\n", MAX(j, k)); + printf("%d is even: %d\n", j, IS_EVEN(j)); + printf("%d is even? %s\n", j, IS_EVEN_STR(j)); } diff --git a/c_modern_approach/Preprocessor/playground.c b/c_modern_approach/Preprocessor/playground.c index 048d3aa..c669fff 100644 --- a/c_modern_approach/Preprocessor/playground.c +++ b/c_modern_approach/Preprocessor/playground.c @@ -1,9 +1,10 @@ #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); +int main(void) +{ + unsigned long int k = 10; + printf("sizeof k: %lu", k); + int x = sizeof(int); + printf("sizeof k: %d", x); } diff --git a/c_modern_approach/Preprocessor/predefined.c b/c_modern_approach/Preprocessor/predefined.c new file mode 100644 index 0000000..e98b1d4 --- /dev/null +++ b/c_modern_approach/Preprocessor/predefined.c @@ -0,0 +1,8 @@ +#include +#include +int main() { + // printf("%s", __STDC_IEC_559__); + // printf("%s", __STDC_IEC_559_COMPLEX__); + printf("%d\n", __STDC_VERSION__); + printf("%d", __STDC_HOSTED__); +}; diff --git a/c_modern_approach/Preprocessor/single_hashtag_op.c b/c_modern_approach/Preprocessor/single_hashtag_op.c new file mode 100644 index 0000000..3045f77 --- /dev/null +++ b/c_modern_approach/Preprocessor/single_hashtag_op.c @@ -0,0 +1,11 @@ +#include +#define PRINT_INT(n) printf(#n " = %d\n", n) + +int main() { + int i = 10; + PRINT_INT(i); +} + +/* This program with the help of `#` operator will conver #n to string literal + * of an argument, so the output of the program will be: i = 10 + */