views:

210

answers:

2

Write a small C program, which while compiling takes another program from input terminal, and on running gives the result for the second program. (NOTE: The key is, think UNIX).

Suppose, the program is 1.c Then, while compiling

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

The most reasonable way to make compilation read a file would be #include, but it's not obvious to me how to make it read standard input in a portable way on all Unix systems (easy in Linux, thanks to the magic of /proc!, but that wouldn't be portable).

Alex Martelli
+6  A: 

This is an old parlaour trick I guess

My program, tty.c:

#include "/dev/tty"

Shell:

$ 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
Falaina
hmm, neat. 1515151515
Carson Myers
Thats really a suprising answer ... never thought there is such a way to do things ...
codingfreak