写一行C程序,其输出是程序本身 [英] write a single line C program whose output is the program itself

查看:59
本文介绍了写一行C程序,其输出是程序本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个愚蠢的问题...(可能是)


我们可以编写单行C程序它的输出是程序

本身?


有人知道答案请告诉我。


Puneet

Hi ALL,

I have a silly question... (may be)

Can we write a single line C program whose output is the program
itself?

Is anybody know the answer please tell me.

Puneet

推荐答案

c标准没有说明程序在内存中的含义。

所以制作一个在每个系统上执行此操作的程序可能无法实现


在某些系统(如Windows)上,如果编译程序时没有任何

优化所有内容通常都会在你编写

程序时结束。这意味着以下内容将在某些

系统上打印出来:


int main(无效)

{

unsigned char * pCur =(unsigned char *)main,* pEnd =(unsigned

char *)emptyfunc;

while(pCur< pEnd )printf("%c",* pCur ++);

}


void emptyfunc(void)

{

返回;

}


这不是你想要做的事情。

得到一个反汇编程序要好得多。


-

bjrnove

The c standard doesn''t say anything about how a program is in memory.
So to make a program that does this on every system will probably not
be possible.
On some systems (like windows), if you compile your program without any
optimisation everything usaly end up in memory almost as you write the
program. This means that the following would print out it self on some
systems:

int main(void)
{
unsigned char *pCur = (unsigned char*)main, *pEnd = (unsigned
char*)emptyfunc;
while(pCur < pEnd) printf("%c", *pCur++);
}

void emptyfunc(void)
{
return;
}

This isn''t something you usaly would want to do. It''s much bether to
get a disassembler.

--
bjrnove


我忘了声明emtyfunc,所以你必须在main之前添加

void emptyfunc();

。 :-)


-

bjrnove

I forgot to declare emtyfunc, so you will have to add
void emptyfunc();
before main. :-)

--
bjrnove


Puneet说,3 / 16/2005 3:53 PM:
Puneet said,on 3/16/2005 3:53 PM:
大家好,

我有一个愚蠢的问题......(可能是)

我们可以写一个单行C程序,其输出是程序本身?

有人知道答案请告诉我。

Puneet
Hi ALL,

I have a silly question... (may be)

Can we write a single line C program whose output is the program
itself?

Is anybody know the answer please tell me.

Puneet



google for Quine ...

你会得到很多这样的程序......


-

Shanmu。



google for Quine...
you''ll get lots of such programs...

--
Shanmu.


这篇关于写一行C程序,其输出是程序本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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