这四行棘手C $ C $的C概念背后 [英] Concept behind these four lines of tricky C code

查看:174
本文介绍了这四行棘手C $ C $的C概念背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个code给输出 C ++吮吸?背后是什么概念呢?

Why does this code give the output C++Sucks? What is the concept behind it?

#include <stdio.h>

double m[] = {7709179928849219.0, 771};

int main() {
    m[1]--?m[0]*=2,main():printf((char*)m);    
}

测试一下这里

推荐答案

7709179928849219.0 具有以下二重presentation为64位双击

The number 7709179928849219.0 has the following binary representation as a 64-bit double:

01000011 00111011 01100011 01110101 01010011 00101011 00101011 01000011
+^^^^^^^ ^^^^---- -------- -------- -------- -------- -------- --------

+ 显示符号的位置; ^ 指数的,而 - 尾数(即不带指数的值)

+ shows the position of the sign; ^ of the exponent, and - of the mantissa (i.e. the value without the exponent).

由于重新presentation使用二进制指数和尾数,加倍的数目加1指数。程序的确是precisely 771倍,因此它在1075开始的指数成为1075 + 771 = 1846末(的 10000110011 小数重presentation) ; 1846年二重presentation是 11100110110 。得到的模式是这样的:

Since the representation uses binary exponent and mantissa, doubling the number increments the exponent by one. Your program does it precisely 771 times, so the exponent which started at 1075 (decimal representation of 10000110011) becomes 1075 + 771 = 1846 at the end; binary representation of 1846 is 11100110110. The resultant pattern looks like this:

01110011 01101011 01100011 01110101 01010011 00101011 00101011 01000011
-------- -------- -------- -------- -------- -------- -------- --------
0x73 's' 0x6B 'k' 0x63 'c' 0x75 'u' 0x53 'S' 0x2B '+' 0x2B '+' 0x43 'C'

该模式对应于你看到印刷,只有向后字符串。与此同时,数组的第二个元素变为零,提供空终止,使得适合传递给的printf()

This pattern corresponds to the string that you see printed, only backwards. At the same time, the second element of the array becomes zero, providing null terminator, making the string suitable for passing to printf().

这篇关于这四行棘手C $ C $的C概念背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆