3 ms·
I was curious, so I tested that[0], and 3 of the 4 major compilers (clang, gcc, and ICC) all output `(nil)` for a `%p` on a null pointer, but `(null)` for a `%s
by colejohnson66 6y ago
I was curious, so I tested that[0], and 3 of the 4 major compilers (clang, gcc, and ICC) all output `(nil)` for a `%p` on a null pointer, but `(null)` for a `%s`. Godbolt doesn't support executing MSVC for some reason.
However, it is my understanding that passing a non-null pointer to printf for `%s` is undefined behavior.
[0]: https://godbolt.org/z/7K5qWv https://godbolt.org/z/7K5qWv
- saagarjha 6y agoAll three are probably using glibc, which does that, yes: https://github.com/ahjragaas/glibc/blob/82cfac84c7e24be587bb0b56c4ef2c80f7677aae/stdio-common/vfprintf-internal.c#L1037 https://github.com/ahjragaas/glibc/blob/82cfac84c7e24be587bb.... On Darwin Apple’s libc prints “(null)”: https://github.com/apple-open-source-mirror/Libc/blob/5e566be7a7047360adfb35ffc44c6a019a854bea/stdio/FreeBSD/vfprintf.c#L957 https://github.com/apple-open-source-mirror/Libc/blob/5e566b.... I should also note that passing a non-null pointer to printf is the only correct way to use it ;)
- MauranKilom 6y ago> Godbolt doesn't support executing MSVC for some reason. That's because the MSVC compiler doesn't actually run on the godbolt servers (unlike all the other ones). The compilation is done on Microsoft servers. It's understandable that neither side is looking to have Windows sandboxes to execute arbitrary code in. (Disclaimer: At least that's how it worked when Matt introduced the MSVC compilers. Not sure if things are substantially different by now.)