To be clear, I was making a distinction between undefined behavior and implementation defined behavior, and unspecified behavior is something else again. But I'll try to explain it:
My understanding of unspecified behavior is that it's more or less just implementation defined behavior, except that there's no requirement that the implementation documents its behavior.
Example of implementation-defined behavior: int x = 10; unsigned char buf[4]; memcpy(buf, &x, 4); -- it's implementation-defined whether buf ends up as {10, 0, 0, 0} (little endian byte order) or {0, 0, 0, 10} (big endian byte order), but the implementation must document which byte order it uses. You can read the documentation of GCC-for-x86_64-Linux and see that it uses little endian byte order, and therefore know that you end up with {10, 0, 0, 0}.
Example of unspecified behavior: The order which expressions are evaluated in function calls. The expression my_cool_function(printf("Hello"), printf("World"));can result in printing HelloWorld or printing WorldHello depending on the evaluation order, and the implementation is not required to document which it will be.
Invoking both unspecified behavior and implementation-defined behavior is perfectly fine (and honestly unavoidable; you can't avoid calling functions which multiple parameters, and that includes unspecified behavior). Though it's a good idea to not rely on unspecified or implementation-defined behavior where possible; i.e write your program such that it behaves the same whether parameters are evaluated left to right or right to left.
Invoking undefined behavior is a land mine which blows your legs off.
View all comments
Show parent comment
4
u/mort96 Apr 01 '26 edited Apr 01 '26
To be clear, I was making a distinction between undefined behavior and implementation defined behavior, and unspecified behavior is something else again. But I'll try to explain it:
My understanding of unspecified behavior is that it's more or less just implementation defined behavior, except that there's no requirement that the implementation documents its behavior.
Example of implementation-defined behavior:
int x = 10; unsigned char buf[4]; memcpy(buf, &x, 4);-- it's implementation-defined whetherbufends up as{10, 0, 0, 0}(little endian byte order) or{0, 0, 0, 10}(big endian byte order), but the implementation must document which byte order it uses. You can read the documentation of GCC-for-x86_64-Linux and see that it uses little endian byte order, and therefore know that you end up with{10, 0, 0, 0}.Example of unspecified behavior: The order which expressions are evaluated in function calls. The expression
my_cool_function(printf("Hello"), printf("World"));can result in printingHelloWorldor printingWorldHellodepending on the evaluation order, and the implementation is not required to document which it will be.Invoking both unspecified behavior and implementation-defined behavior is perfectly fine (and honestly unavoidable; you can't avoid calling functions which multiple parameters, and that includes unspecified behavior). Though it's a good idea to not rely on unspecified or implementation-defined behavior where possible; i.e write your program such that it behaves the same whether parameters are evaluated left to right or right to left.
Invoking undefined behavior is a land mine which blows your legs off.