Created
December 5, 2023 21:04
-
-
Save Moderrek/32a1c7605546e117f5679c8a8398b0ea to your computer and use it in GitHub Desktop.
Struct printf macro and explain C compile time str concatenation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Tymon Woźniak <https://github.com/Moderrek> | |
C compile time concatenation | |
Struct printf | |
*/ | |
#include <stdio.h> | |
// struct print format | |
#define Point_Fmt "(%d, %d)" | |
// unpack struct | |
#define Point_Arg(p) p.x, p.y | |
typedef struct { | |
int x, y; | |
} Point; | |
int main() { | |
Point p = { .x = 5, .y = 2 }; | |
// C compile time concatenation | |
printf("Compile""Time :D\n"); | |
// Cool format | |
printf("Point "Point_Fmt"\n", Point_Arg(p)); | |
// ^^ ^^ ^ | |
// Compile time concat Unpack | |
// bad | |
printf("Point (%d, %d)\n", p.x, p.y); | |
// imagine a much more complicated structure and writing it out at every moment of the program | |
// plus struct change :) | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment