Reputation: 21
I found code in which a macro is defined as #define VAR {0}
.
Does it have any special implication or is it the same as defining the macro as zero?
Code:
#define VAR {0}
Upvotes: 0
Views: 373
Reputation: 215257
In C, {0}
is the "universal zero initializer" that can be used to initialize any object in the same way it would be initialized if it had static storage duration and did not have an explicit initializer. There are a few places in the standard library (mbstate_t
) and third-party libraries where a zero-initialized object is required but the object is semantically opaque, such that it would be non-portable or anti-idiomatic to explicitly initialize members by name or even to assume that it has members (e.g. it might just be defined as a bare integer or pointer type). {0}
works for all of these.
One special case of this is use to create dummy compount literals, i.e. (type){0}
. These are useful to pass to functions that don't accept null pointers when you want to throw the output away, since you can pass &(type){0}
.
Otherwise, I'm not aware of anywhere {0}
is useful.
Defining a macro for this, and naming it VAR
, is rather unusual, and it's not clear what the author meant by that without more context.
Upvotes: 2
Reputation: 85767
#define VAR {0}
Does it have any special implication
No.
... or is it the same as defining the macro as zero?
No.
There is no magic here. VAR
will literally expand to {0}
.
Upvotes: 3
Reputation: 901
It looks like the macro is used to prevent uninitialized variables since it may lead to undefined behavior. May be used like this:
#define VAR {0}
double var1 VAR;
struct some_struct s1 VAR;
char * ptr1 VAR;
Event though the macro may have slightly another meaning or field of use, my assumption is based on its name.
Upvotes: -1