Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write program during compiling?

Tags:

c

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
$
like image 535
extraeee Avatar asked Oct 03 '09 05:10

extraeee


People also ask

What is the process of compiling a program?

Compiling a C program is a multi-stage process. At an overview level, the process can be split into four separate stages: Preprocessing, compilation, assembly, and linking.

What are the four steps of compiling?

Compilation process in C involves four steps: pre-processing, compiling, assembling, and linking. The preprocessor tool helps in comments removal, macros expansion, file inclusion, and conditional compilation. These commands are executed in the first step of the compilation process.


1 Answers

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
like image 98
Falaina Avatar answered Sep 20 '22 04:09

Falaina