你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

C语言1987年的获奖作品让我觉得很好玩

[复制链接]
gaosmile 发布时间:2020-4-30 15:51
今天翻前两天刚送到的《C专家编程》,章节8.10的那个IOCCC 1987年的获奖作品让我觉得很好玩。
  1. 9 n! B& W1 E8 k* ?( O; w- i  u: o
  2. main(){ printf(&unix["/021%six/012/0"], (unix)["have"] + "fun" - 0x60);}
复制代码
' Y5 J+ w, M6 A4 m# h% |$ k, ]
作者这里利用的第一个技巧并不算太晦涩:"a = i[a]=  *(a+i)“ 即下标运算符的可交换性。
. m$ r5 m1 c% r$ C5 G3 G
但是这个老天爷的unix和&unix是怎么回事?
$ {% L3 a& Z: _/ x' ]
我把自己脑子里有印象的C的最隐蔽的角落回想了一遍,难道是trigraph之类的特殊符号?查了查不是
( w2 \3 h1 a' w; C2 q& ~7 U
正推不行,就反推吧。编译运行后,输出结果是“unix“。如此说来“(unix)["have"] + "fun" - 0x60)“这个表达式的结果应该是指向"un"的char * ;因此,“(unix)["have"] - 0x60"的值应该是整数1才对;因此“(unix)["have"]"的值应该是0x61;这就豁然开朗了,0x61='a',是"have"中的第二个字符,所以unix的值必然为1。

& X- |% s) N: S" A( u
这个反推是否正确呢? 利用gcc看一下只进行preprocessor处理后的代码:

  1. - l! j0 a2 |3 b
  2. whodare@whodare:~/programming/c++$ gcc -E 1.c
    ' z. K" e3 \+ [
  3. main()
    7 v) o& @: O1 Z* @& x8 v
  4. {
    . u* E. j1 L; B: d4 X" z
  5.     printf(&1["/021%six/012/0"], (1)["have"] + "fun" - 0x60) ;& l- ^* H2 i% B0 w0 q
  6. }
复制代码

1 A; P3 I" H4 v2 U/ n% w
5 W8 N2 v0 w6 L2 `/ B% I  @' r
正确。unix看来是属于预定义的marco,值为1。拿“gcc predefined marco"为关键字google了一下,找到可信资料了:
http://gcc.gnu.org/onlinedocs/cpp/System_002dspecific-Predefined-Macros.html#System_002dspecific-Predefined-Macros
6 x4 H) L5 Z8 ]1 e  B
原来unix这个宏是属于gcc提供的 System-specific Predefined Macros,因此这个奇妙的函数只能在*nix环境下正常工作。

% ~( j9 I" D! ?; g
可以查看更多的gcc提供的predefined marco
  1. & x2 }& g1 X- q9 q
  2. gcc -dM -E 1.c
复制代码
2 p6 f+ Q2 f. I- ^
顺便附上历史文献,哈哈
  1. & a& z/ }; o# D  O! r0 R# Z
  2. Best One Liner:/ D' s1 O7 L/ I$ k* `( u
  3. : G# u3 W4 r! z2 s" y

  4. 7 {7 J2 H4 F2 x
  5.     David Korn
    9 ~. f+ {$ s( o; B0 A2 a; R
  6.     AT&T Bell Labs
    8 |  r9 V- i& V6 `* K' N: a+ V4 f
  7.     MH 3C-526B, AT&T Bell Labs9 @  n3 y& P: o4 I5 y
  8.     Murray Hill, NJ/ _: ~' O; w& L" n# [
  9.     07974% _. _: S; O' \$ Y  r7 l
  10.     USA
    * D$ R8 L: U* s# U) \

  11. ) n: w, y5 D* I9 ]0 Z
  12.    The Judges believe that this is the best one line entry ever received.* P+ P1 N/ V1 d! Y. g
  13. Compile on a UN*X system, or at least using a C implementation that2 z) b5 U, S; a/ t4 U$ A8 z/ W
  14. fakes it.  Very few people are able to determine what this program, H9 z$ \2 X5 ?; X8 W
  15. does by visual inspection.  I suggest that you stop reading this
    # l2 d1 t+ D1 N1 t- Q! o2 e
  16. section right now and see if you are one of the few people who can.0 B1 O( O$ }+ R8 C
  17. " ^3 j. w! M. L2 e

  18. ; t3 e- N" [/ P/ F1 {1 G% B- m
  19. Several points are important to understand in this program:
    2 O$ K5 U8 g, j2 p7 k2 A
  20. " {6 v6 Y) |; R" s& T' V; r
  21. / ^/ D1 h( A2 S7 W/ [
  22.     1) What is the symbol `unix' and what is its value in the program?8 I& c" O0 v! q; g0 S
  23.        Clearly `unix' is not a function, and since `unix' is not declared
    - v. J) t% s) \. a: E
  24.        to be a data type (such as int, char, struct foo, enum, ...); B1 e$ [: ^8 h
  25.        what must `unix' be?
    # W- t$ I2 q; N* w3 X- C' n

  26. & {  n+ N# t, a# e1 F7 e$ i9 f

  27. 6 y" P5 s( e$ h' K1 g. G
  28.     2) What is the value of the symbol "have"?  (hint: the value is- n# j% i1 j# Z- Y
  29.        NOT 4 characters, or 'h', or a string)  Consider the fact that:( D9 O3 L. v& Z5 z! A

  30. 9 |# _, }, e3 S8 g% u/ [

  31. # _& [' _. B, r' V$ d! P
  32.         char *x;0 `( U2 M8 k; a7 P5 G1 |0 W
  33. - s( I7 m! H# M9 s5 Q; U2 S/ Q
  34. ( K+ w5 y6 }6 w( v( G1 a
  35.       defines a pointer to a character (i.e. an address), and that
    : G  N0 ^* I+ q% \" C3 x
  36.       the `=' assigns things is compatible types.  Since:" C+ S! w' r$ _( c  M& j
  37. " R. J- \1 a) i' a
  38. ; w9 Y. t: y5 f
  39.             x = "have";
    8 U+ o* P. f" N, o. n+ X) }

  40. 7 O4 d- J. c6 u/ V2 n

  41. - o% \+ b( x* w. a) _/ w" y
  42.       is legal C, what type of value is "have"?
    + ~; I- j" V( g4 ?8 W/ q" z

  43. ) k& i2 u& i9 f

  44. ! A; [: s% Q8 Z7 ^
  45.     3) Note that the following expressions yield the same value:% L7 D+ b5 {; R
  46. (unix)["have"] + "fun" - 0x60)! X$ Z( i! v9 f2 w
  47.         x[3]    *(x+3)      *(3+x)
    9 h! y+ L" f9 n. |. S$ s) C: s; I
  48. 8 c" e2 o4 U; n7 m& l
  49. , p) t3 l4 F" x% x" ]
  50.        since addition is communitive.  What can be said about the value:( e: m4 C* ?1 @1 v

  51. 4 s; C9 J1 k" g+ U! ^+ n. P; T

  52. % W- S/ M) M# Q* n2 r
  53.         3[x]
复制代码

9 c0 Z- ]/ ^4 L2 Q

# a* g- }7 P$ @9 z
赞 收藏 评论0 发布时间:2020-4-30 15:51

举报

0个回答

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版