技术宅,fat-man

增加语言的了解程度可以避免写出愚蠢的代码

导航

2012年4月25日 #

能打印自己源码的C程序的版本2

摘要: 版本2#include <stdio.h>;int main(){ FILE *f; char c; if ( ( f = fopen( __FILE__, "r" ) ) != NULL ) { while ( fread( &c, 1, 1, f ) == 1 ) fwrite( &c, 1, 1, stdout ); fclose( f ); } return 0;} 阅读全文

posted @ 2012-04-25 23:48 codestyle 阅读(169) 评论(0) 推荐(0)

能打印自己源码的C程序的版本1

摘要: 版本1#include <stdio.h>int main(){char *p ="#include <stdio.h>%c int main(){char *p=%c%s%c; printf(p, 10, 34, p, 34);}";printf(p, 10, 34, p, 34);}评:这个实现非常精妙, 第一个p将当前字符串传递进去,作为printf的格式字符串,用后面的参数来替换格式字符串中的占位符(%c ,%c%s%c)生成一个新的字符串,第一个%c被替换为回车符(10的ASCII码值)%c%s%c则被替换为:"#include 阅读全文

posted @ 2012-04-25 22:30 codestyle 阅读(517) 评论(0) 推荐(0)