C語(yǔ)言混亂大賽1987年獲獎(jiǎng)作品
今天翻前兩天剛送到的《C專家編程》,章節(jié)8.10的那個(gè)IOCCC 1987年的獲獎(jiǎng)作品讓我覺(jué)得很好玩。
main(){ printf(&unix["/021%six/012/0"], (unix)["have"] + "fun" - 0x60);}
作者這里利用的第一個(gè)技巧并不算太晦澀:"a[i] = i[a]= *(a+i)“ 即下標(biāo)運(yùn)算符的可交換性。
但是這個(gè)老天爺?shù)膗nix和&unix是怎么回事?
我把自己腦子里有印象的C的最隱蔽的角落回想了一遍,難道是trigraph之類的特殊符號(hào)?查了查不是
正推不行,就反推吧。編譯運(yùn)行后,輸出結(jié)果是“unix“。如此說(shuō)來(lái)“(unix)["have"] + "fun" - 0x60)“這個(gè)表達(dá)式的結(jié)果應(yīng)該是指向"un"的char * ;因此,“(unix)["have"] - 0x60"的值應(yīng)該是整數(shù)1才對(duì);因此“(unix)["have"]"的值應(yīng)該是0x61;這就豁然開(kāi)朗了,0x61='a',是"have"中的第二個(gè)字符,所以u(píng)nix的值必然為1。
這個(gè)反推是否正確呢? 利用gcc看一下只進(jìn)行preprocessor處理后的代碼:
whodare@whodare:~/programming/c++$ gcc -E 1.cmain(){printf(&1["/021%six/012/0"], (1)["have"] + "fun" - 0x60) ;}
正確。unix看來(lái)是屬于預(yù)定義的marco,值為1。拿“gcc predefined marco"為關(guān)鍵字google了一下,找到可信資料了:
http://gcc.gnu.org/onlinedocs/cpp/System_002dspecific-Predefined-Macros.html#System_002dspecific-Predefined-Macros
原來(lái)unix這個(gè)宏是屬于gcc提供的 System-specific Predefined Macros,因此這個(gè)奇妙的函數(shù)只能在*nix環(huán)境下正常工作。
可以查看更多的gcc提供的predefined marco
gcc -dM -E 1.c
順便附上歷史文獻(xiàn),哈哈
Best One Liner:David KornBell LabsMH 3C-526B, AT&T Bell LabsMurray Hill, NJ07974USAThe Judges believe that this is the best one line entry ever received.Compile on a UN*X system, or at least using a C implementation thatfakes it. Very few people are able to determine what this programdoes by visual inspection. I suggest that you stop reading thissection right now and see if you are one of the few people who can.Several points are important to understand in this program:What is the symbol `unix' and what is its value in the program?Clearly `unix' is not a function, and since `unix' is not declaredto be a data type (such as int, char, struct foo, enum, ...)what must `unix' be?What is the value of the symbol "have"? (hint: the value isNOT 4 characters, or 'h', or a string) Consider the fact that:char *x;defines a pointer to a character (i.e. an address), and thatthe `=' assigns things is compatible types. Since:x = "have";is legal C, what type of value is "have"?Note that the following expressions yield the same value:+ "fun" - 0x60)*(x+3) *(3+x)since addition is communitive. What can be said about the value:3[x]
本文授權(quán)轉(zhuǎn)載自公眾號(hào)“技術(shù)讓夢(mèng)想更偉大”,作者李肖遙
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺(tái)僅提供信息存儲(chǔ)服務(wù)。文章僅代表作者個(gè)人觀點(diǎn),不代表本平臺(tái)立場(chǎng),如有問(wèn)題,請(qǐng)聯(lián)系我們,謝謝!






