18

Possible Duplicate:
How to write program during compiling?

I found this problem on a site full of interview questions, and was stumped by it. Is there some preprocessor directive that allows one to read from standard input during compilation?

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

EDIT It turns out this question is an exact duplicate. How to write program during compiling?

peterh
  • 11,875
  • 18
  • 85
  • 108
Brennan Vincent
  • 10,736
  • 9
  • 32
  • 54

3 Answers3

60

#include "/dev/stdin" is the trick.

A silly interview question at best.

ohmantics
  • 1,799
  • 14
  • 16
  • 28
    Wooooow. That is... evil. – detly Jan 14 '11 at 03:32
  • 1
    Correct answer, unnecessary comment though – Ramon Zarazua B. Jan 14 '11 at 03:39
  • 18
    "Silly" does not begin to describe it - it's of the severity of idiotic that the asker should be taken out and summarily shot. – Lawrence Dol Jan 14 '11 at 05:08
  • 2
    @Software Monkey, I don't think that suggesting physical violence is appropriate for a site like this, even if meant ironical. – Jens Gustedt Jan 14 '11 at 07:49
  • 1
    @Jens Gustedt, how about using geek code and some regex instead? w--- =~ s/bill gates/the interviewer/ig; Is it allowed to output the result? – Nylon Smile Jan 14 '11 at 09:38
  • 1
    @ohmantics: How do you feel about the fact that this is your highest scoring answer by miles? – JeremyP Jan 14 '11 at 11:10
  • 2
    @JeremyP I think my suspicions about how points are earned on SO are true. Funny/trivia Q&A are top of the heap, business grind stuff like VB, Java, ASP, C#, etc. are also popular. Difficult things barely get votes. – ohmantics Jan 14 '11 at 21:30
3

In the spirit of one-upmanship, I tried to make a more platform/environment-independent version. However, I got stuck here... so this is my question, what can I replace '???' with?

#ifdef _WIN32
#include ???
#else
#include "/dev/stdin"
#endif

EDIT: Thanks to ohmantics I can now get the job with:

#ifdef _WIN32
#include "CON"
#else
#include "/dev/stdin"
#endif
Community
  • 1
  • 1
William
  • 1,007
  • 7
  • 11
  • 3
    `#include "CON"` is the trick there. – ohmantics Jan 14 '11 at 03:45
  • 3
    I remember the days Internet Explorer would BSOD your computer if it encountered something like ``. – zneak Jan 14 '11 at 04:09
  • @zneak Which version was that? Terrible design. The website should never be able to access files without the user's permission. – Mateen Ulhaq Jan 14 '11 at 05:50
  • @mutoo It was somewhere in the late 1990s. I don't remember the rest, I was barely ten years old. – zneak Jan 14 '11 at 19:23
  • 1
    @muntoo Also, I think all major browsers allowed file access like that because there was no reason to worry anyone could do anything useful with it. JavaScript was just stupidly useless back then. – zneak Jan 14 '11 at 19:34
  • @muntoo http://en.wikipedia.org/wiki/Concon – styfle Aug 27 '11 at 00:42
1

You need to tell the compiler to take its source code input from the standard input, and compile that. There is very probably a command line argument for that.

This way, you can pipe the output of another program into your compiler.

EDIT As usual, Stack Overflow already had an answer for this.

echo "int main() { return 0; }" | gcc -x c -

EDIT Missed the while compiling statement. The piping trick still works otherwise though, so I'll leave it there.

Community
  • 1
  • 1
zneak
  • 134,922
  • 42
  • 253
  • 328