a lot of people don't know this - C actually lets you do arithmetic with mixed types, much like JavaScript: pic.twitter.com/6XF4qm41xv
— luna 💙 (@lunasorcery) March 18, 2022
This also works for exponents: pic.twitter.com/LYBf2qkseO
— luna 💙 (@lunasorcery) March 18, 2022
第一個的 "-0.5"
是 char[]
,補了 + 1
會往後一格,所以會變成移到 "0.5"
的部份。
所以如果改成 -0.6
,你會發現輸出變成 0.6
。
第二個的 50 ** "2"
則是利用了 2
的 ascii code 是 0x32,換成十進制剛好是 50,然後中間的 **
其實是一個乘號與一個 pointer 的用法,實際上剛好會是 50*50=2500 的運算。
如果你改成 "3"
的話會輸出 2550
。
作者在 Twitter 的後續有提到,這些都是特別挑過的數字所造成的「巧合」,你換掉這些數字的話通常會爛掉
(除非你也很精心挑過),不要誤解亂用 XDDD
To clarify:
Both of these are joke posts. C doesn’t actually do either of these things in the general case - I’m (mis)using other features of the language with carefully chosen numbers to make it seem like it works.— luna 💙 (@lunasorcery) March 19, 2022