Interesting problems(1) - Write a program to print itself

The C implementation is as follows:

#include<stdio.h>
int main(){printf("#include<stdio.h>\n");char *p="int main(){printf(%c#include<stdio.h>%cn%c);char *p=%c%s%c;printf(p,34,92,34,34,p,34);return 0;}";printf(p,34,92,34,34,p,34);return 0;}

The trick here is the string 'p'. In order to print the string itself, we use a '%s' in the string. This character split the string into two parts. The first part is:

int main(){printf("#include<stdio.h>\n");char *p="

And the second is:

";printf(p,34,92,34,34,p,34);return 0;}

And the character '%s' represents the string itself. With the help of C-function 'printf', we then can easily obtain the solution.

 

Moreover, I've seen lots of other solutions on the internet. Some of them just use file operations to print the source code file to the screen. It is really bull shit. What if source code file is forbidden?

 

The other solutions just provide a pile of unrecognizable characters. In my opinion, these code is not beautiful enougth, for they destroy the beauty of programming. So if u find any better solutions, please contact me. ^_^

posted on 2010-03-15 00:12  BloodElf 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/BloodElf/archive/2010/03/15/1685830.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值