Preprocessor chapter recap

This commit is contained in:
t0xa 2025-10-06 22:54:40 +03:00
parent 84071736e2
commit f4401884b8
6 changed files with 46 additions and 11 deletions

View file

@ -2,14 +2,17 @@
#include <stdio.h> #include <stdio.h>
#define FREEZING_PT 32.0f #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; float farenheit, celsius;
printf("Enter Fahrenheit temperature: "); printf("Enter Fahrenheit temperature: ");
scanf("%f", &farenheit); scanf("%f", &farenheit);
#define SCALE_FACTOR (5.0f / 9.0f)
celsius = (farenheit - FREEZING_PT) * SCALE_FACTOR; celsius = (farenheit - FREEZING_PT) * SCALE_FACTOR;
printf("Celcius equivalent is: %.1f\n", celsius); printf("Celcius equivalent is: %.1f\n", celsius);

View file

@ -1,7 +1,15 @@
#include <stdio.h> #include <stdio.h>
#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 main(int argc, char *argv[]) {
int MK_ID(1); // This converts to `int i1` int MK_ID(1); // This converts to `int kakulya_1`
printf("%d", i1); printf("%d", kakulya_1);
} }

View file

@ -1,9 +1,13 @@
#include <stdio.h> #include <stdio.h>
#define MAX(x, y) ((x) > (y) ? (x) : (y)) #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 main(int argc, char *argv[]) {
int j, k; int j, k;
j = 10; j = 10;
k = 20; 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));
} }

View file

@ -1,9 +1,10 @@
#include <stdio.h> #include <stdio.h>
#define PRINT_INT(n) printf(#n " AND " #n " = %d\n", n) #define PRINT_INT(n) printf(#n " AND " #n " = %d\n", n)
int main(void) { int main(void)
int i, j; {
i = 10; unsigned long int k = 10;
j = 20; printf("sizeof k: %lu", k);
PRINT_INT(i / j); int x = sizeof(int);
printf("sizeof k: %d", x);
} }

View file

@ -0,0 +1,8 @@
#include <stdc-predef.h>
#include <stdio.h>
int main() {
// printf("%s", __STDC_IEC_559__);
// printf("%s", __STDC_IEC_559_COMPLEX__);
printf("%d\n", __STDC_VERSION__);
printf("%d", __STDC_HOSTED__);
};

View file

@ -0,0 +1,11 @@
#include <stdio.h>
#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
*/