Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a preprocessor directive to read source from standard input during compilation? [duplicate]

Tags:

c

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?

like image 341
Brennan Vincent Avatar asked Jan 14 '11 03:01

Brennan Vincent


People also ask

What do preprocessor directive #if and #endif explain?

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.

What is preprocessor and their use in C?

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.

Which is not a preprocessor directive?

Ans: #elseif is not any preprocessor directive, but #elif is.


2 Answers

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

A silly interview question at best.

like image 179
ohmantics Avatar answered Oct 14 '22 03:10

ohmantics


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
like image 21
William Avatar answered Oct 14 '22 03:10

William