问题描述
我发现 C
code的prints从1到1000,而不循环或条件的:
但我不明白它是如何工作的。任何人都可以顺利通过code和解释每行?
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;无效的主要(诠释J){
的printf(%d个\\ N,J);
(安培;主+(安培;出口 - &安培;主)*(J / 1000))(J + 1);
}
永远不要写code类的。
有关 J< 1000
,焦耳/ 1000
是零(整数除法)。所以:
(安培;主+(安培;出口 - &安培;主)*(J / 1000))(J + 1);
相当于:
(安培;主+(安培;出口 - &安培;主)* 0)(J + 1);
这是:
(安培;主)(J + 1);
其中要求主
与 J + 1
。
如果Ĵ== 1000
,那么同样的思路出来的:
(安培;主+(安培;出口 - &安培;主)* 1)(J + 1);
这可以归结为
(安培;出口)(J + 1);
这是退出(J + 1)
并保留节目。
(安培;出口)(J + 1)
和退出(J + 1)
基本上是相同的东西 - 报价C99§6.3.2.1/ 4:
exit
is a function designator. Even without the unary &
address-of operator, it is treated as a pointer to function. (The &
just makes it explicit.)
And function calls are described in §6.5.2.2/1 and following:
So exit(j+1)
works because of the automatic conversion of the function type to a pointer-to-function type, and (&exit)(j+1)
works as well with an explicit conversion to a pointer-to-function type.
That being said, the above code is not conforming (main
takes either two arguments or none at all), and &exit - &main
is, I believe, undefined according to §6.5.6/9:
The addition (&main + ...)
would be valid in itself, and could be used, if the quantity added was zero, since §6.5.6/7 says:
So adding zero to &main
would be ok (but not much use).
这篇关于如何在C code,它从1打印到1000没有循环或条件语句工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!