* G. Branden Robinson <g.branden.robinson@xxxxxxxxx>, 2022-12-05 11:55:
The following compiles without warnings on my system, even with -Wall.
int main(int argc, char *argv[]) {
wchar_t w1 = '\0', w2 = L'\0';
printf("%d\n", (w1 + w2));
}
For me this reliably writes "0" to the standard output.
However it is conceivable, depending on the implementation, that bits
8+ of w1 come from uninitialized memory, and a large positive or
negative value would be written to stdout.
Er, no? Both assignments and additions in this code are well defined.
You're just operating with zeros of different types, which is completely
fine.
The only potential for undefined behavior is printf, because %d may not
be compatible with wchar_t. In fact, it isn't on my system (i386):
wchar.c:5:18: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘wchar_t’ {aka ‘long int’} [-Wformat=]
--
Jakub Wilk