3 ms·
That’s how C usually prints it. Swift will print “nil”.
by saagarjha 6y ago
That’s how C usually prints it. Swift will print “nil”.
- deathanatos 6y agoHuh. I feel like I distinctly remember it being both one of our mobile clients, and it being the string "(null)". Java, perhaps? (That was our Android app, of course.) Although we did have a desktop app (weirdly) and that was in C++.
- saagarjha 6y agoJava just prints “null” IIRC. To be fair, the “(null)” you’ll usually get from passing a null pointer to printf is implementation defined (really, undefined, but no implementation I’ve seen has done bad things with it).
- JimDabell 6y ago> Huh. I feel like I distinctly remember it being both one of our mobile clients, and it being the string "(null)". Objective-C will do that with the format string "%@" if you pass it nil.
- colejohnson66 6y agoI 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.)