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?
What does preprocessor directive #if and #endif explains? Explanation: The #if and #endif directives enable conditional compilation of a sequence of code based upon whether an expression involving one or more symbols evaluates to true. A symbol is true if it has been defined.
The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.
Ans: #elseif is not any preprocessor directive, but #elif is.
#include "/dev/stdin"
is the trick.
A silly interview question at best.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With