7

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
$
Tom
  • 43,810
  • 29
  • 138
  • 169
extraeee
  • 3,096
  • 5
  • 27
  • 28

2 Answers2

15

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
  • 6,625
  • 29
  • 31
1

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
  • 854,459
  • 170
  • 1,222
  • 1,395