As far as I can tell, there's no way to print out a struct value in C.
i.e., this doesn't fly:
typedef struct {
int a;
double b;
} stype
stype a;
a.a=3;
a.b=3.4;
printf("%z", a);
instead you have to say:
printf("a: %d\n", a.a);
printf("b: %f\n", a.b);
This seems like a perfect place where you could use a macro to save a vast amount of typing for arbitrary structs.
Is the C preprocessor powerful enough to perform this transformation?
You could make macro for this:
and then print it like this:
You might wanna define multiple of these to cover different types (e.g. floats, doubles, print as hex). Unfortunately there is no good workaround for this as C preprecossor has no notion of what types are, e.g. you can't use something like
switch(typeof(M)) { case int: printf(.."%d"..; break; ...}
.No, the C preprocessor is mostly a textual macro replacement tool. It doesn't know about types and structures of C.
Yes, it is, but then you have to repeat the entire struct declaration within the macro which kind of defeats the purpose. You could have something like this:
and then you would need a pretty complex implementation of such macro, with lots and lots of helper macros/functions.
I think that the simplest solution (and maybe the most beautiful) is to use a function to print your specific struct.
If your struct changed, you can adapt in one place your code easily.
You cannot iterate on struct members in C, either dynamically or statically (nor in C++). There is no reflection in C.
Thus, there is no way to make the preprocessor perform this transformation.
I would make two macros, like this:
Then you can do something like:
What makes this approach superior to a "print function" for the structure is that you can use the macros with any of the
printf
-family functions you like, and combine printing of other data.You could get even fancier and instead do:
and then you can use the default precision or choose a custom precision.