如何在编译期间编写程序?

编写一个小的C程序,在编译时从输入终端接收另一个程序,并在运行时给出第二个程序的结果。 (注意:关键是,想想UNIX)。

假设,程序是1.c然后,编译时

$ cc -o 1 1.c int main() { printf("Hello World\n"); } ^D $ ./1 Hello World $ 

我猜这是一个古老的风骚

我的程序,tty.c:

 #include "/dev/tty" 

贝壳:

 $ gcc tty.c int main() { printf("Hey\n"); } *Ctrl-D here* In file included from tty.c:1: /dev/tty: In function 'main': /dev/tty:2: warning: incompatible implicit declaration of built-in function 'printf' $./a.out Hey 

使编译读取文件的最合理的方法是#include ,但对于我来说,如何在所有Unix系统上以可移植的方式读取标准输入并不明显(在Linux中很容易,感谢/proc !的魔力,但这不便携)。