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

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

[复制链接]
gaosmile 发布时间:2020-4-30 15:51
今天翻前两天刚送到的《C专家编程》,章节8.10的那个IOCCC 1987年的获奖作品让我觉得很好玩。

  1. main(){ printf(&unix["/021%six/012/0"], (unix)["have"] + "fun" - 0x60);}
复制代码

作者这里利用的第一个技巧并不算太晦涩:"a = i[a]=  *(a+i)“ 即下标运算符的可交换性。

但是这个老天爷的unix和&unix是怎么回事?

我把自己脑子里有印象的C的最隐蔽的角落回想了一遍,难道是trigraph之类的特殊符号?查了查不是

正推不行,就反推吧。编译运行后,输出结果是“unix“。如此说来“(unix)["have"] + "fun" - 0x60)“这个表达式的结果应该是指向"un"的char * ;因此,“(unix)["have"] - 0x60"的值应该是整数1才对;因此“(unix)["have"]"的值应该是0x61;这就豁然开朗了,0x61='a',是"have"中的第二个字符,所以unix的值必然为1。

这个反推是否正确呢? 利用gcc看一下只进行preprocessor处理后的代码:

  1. whodare@whodare:~/programming/c++$ gcc -E 1.c
  2. main()
  3. {
  4.     printf(&1["/021%six/012/0"], (1)["have"] + "fun" - 0x60) ;
  5. }
复制代码


正确。unix看来是属于预定义的marco,值为1。拿“gcc predefined marco"为关键字google了一下,找到可信资料了:
http://gcc.gnu.org/onlinedocs/cpp/System_002dspecific-Predefined-Macros.html#System_002dspecific-Predefined-Macros

原来unix这个宏是属于gcc提供的 System-specific Predefined Macros,因此这个奇妙的函数只能在*nix环境下正常工作。

可以查看更多的gcc提供的predefined marco

  1. gcc -dM -E 1.c
复制代码

顺便附上历史文献,哈哈

  1. Best One Liner:


  2.     David Korn
  3.     AT&T Bell Labs
  4.     MH 3C-526B, AT&T Bell Labs
  5.     Murray Hill, NJ
  6.     07974
  7.     USA

  8.    The Judges believe that this is the best one line entry ever received.
  9. Compile on a UN*X system, or at least using a C implementation that
  10. fakes it.  Very few people are able to determine what this program
  11. does by visual inspection.  I suggest that you stop reading this
  12. section right now and see if you are one of the few people who can.


  13. Several points are important to understand in this program:


  14.     1) What is the symbol `unix' and what is its value in the program?
  15.        Clearly `unix' is not a function, and since `unix' is not declared
  16.        to be a data type (such as int, char, struct foo, enum, ...)
  17.        what must `unix' be?


  18.     2) What is the value of the symbol "have"?  (hint: the value is
  19.        NOT 4 characters, or 'h', or a string)  Consider the fact that:


  20.         char *x;


  21.       defines a pointer to a character (i.e. an address), and that
  22.       the `=' assigns things is compatible types.  Since:


  23.             x = "have";


  24.       is legal C, what type of value is "have"?


  25.     3) Note that the following expressions yield the same value:
  26. (unix)["have"] + "fun" - 0x60)
  27.         x[3]    *(x+3)      *(3+x)


  28.        since addition is communitive.  What can be said about the value:


  29.         3[x]
复制代码


收藏 评论0 发布时间:2020-4-30 15:51

举报

0个回答

所属标签

STM32团队

意法半导体微控制器和微处理器拥有广泛的产品线,包含低成本的8位单片机和基于ARM® Cortex®-M0、M0+、M3、M4、M33、M7及A7内核并具备丰富外设选择的32位微控制器及微处理器


最新内容

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版