C Comma notation
Posted: Thu Feb 05, 2004 3:18 pm
Just got back to writing some C and this one has me stuck.
I've been looking in a few header files at the methods other people are using for macros, in particular the stdarg.h file (Someone on the OS dev board mentioned it).
Anyway. The macro that's bugging me follows this format:
And it gets used like:
The macro isn't the problem, I understand exactly what it's doing. The problem is I just don't get what the (void) 0 is doing there. As far as I know that's effectively a null operation, and my hazy memory tells me that , notation goes something like this:
This is where I get hazy. That, I think, would make the macro do something like:
I checked this by writing a little C code:
Sure enough i is set to 2.
So I guess the question here is just what the heck that (void) 0 is achieving? It seems to serve no purpose whatsoever. I took a good long look around the web this afternoon and couldn't find anything explaining it. I know it isn't an isolated thing because I went digging around a whole bunch of header files after finding this and it is used all over the place. I'm guessing it's just such a widely followed practice, or is so obvious, that nobody has bothered writing down WHY it's done.
Any chance one of the C gurus could please give an explanation?
I've been looking in a few header files at the methods other people are using for macros, in particular the stdarg.h file (Someone on the OS dev board mentioned it).
Anyway. The macro that's bugging me follows this format:
Code: Select all
#define a_macro(arg) (arg = (Some value), (void) 0)
Code: Select all
a_macro (arg);
Code: Select all
A = (( B = 20), 50);
Means:
A = 50;
B = 20;
Code: Select all
a_macro (arg);
Gets replaced with:
(arg = (Some value), (void) 0)
Which means:
arg1 = (Some value);
(void) 0;
Code: Select all
int i;
(i=2, (void) 0);
printf("%d", i);
So I guess the question here is just what the heck that (void) 0 is achieving? It seems to serve no purpose whatsoever. I took a good long look around the web this afternoon and couldn't find anything explaining it. I know it isn't an isolated thing because I went digging around a whole bunch of header files after finding this and it is used all over the place. I'm guessing it's just such a widely followed practice, or is so obvious, that nobody has bothered writing down WHY it's done.
Any chance one of the C gurus could please give an explanation?