Using remove() function in C, we can write a program which can destroy itself after it is compiled and executed.
Explanation: This can be done using the remove function in C. Note that, this is done in Linux environment. So, the remove function is fed the first parameter in command line argument i.e. a.out file (executable file) created after compiling . Hence the program will be destroyed.
// CPP program of self destructing output file #include<stdio.h> #include<stdlib.h> int main(int c, char *argv[]) { printf("By the time you run me " "I will be destroyed \n"); // Array of pointers to command line arguments remove(argv[0]); // Note: argv[0] will contain the executable\ // file i.e. 'a.out' return 0; } |
Steps:
- Open the terminal.
- Type the following command on the terminal :
gcc self.c
- This will create the a.out file.
- Type the following command on the terminal :
./a.out
Output:
By the time you run me, I will be destroyed
After the output shown above, the a.out file will be removed. And hence our work is done.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Code Golfing in JavaScript
- Three address code in Compiler
- Code Golfing in Python
- Basic Code Optimizations in C
- Writing OS Independent Code in C/C++
- Hamming Code implementation in Python
- Different Python IDEs and Code Editors
- Code to generate the map of India (with explanation)
- Cracking Google Summer of Code 101
- Code valid in both C and C++ but produce different output
- How to write a running C code without main()?
- Register Allocations in Code Generation
- How to execute PHP code using command line ?
- Python code formatting using Black
- Convert C/C++ program to Preprocessor code
- Optimization Tips for Python Code
- Printing source code of a C program itself
- Speed up Code executions with help of Pragma in C/C++
- Convert C/C++ code to assembly language
- Decimal Equivalent of Gray Code and its Inverse
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

