Self Destructing Code in C
Last Updated :
21 Nov, 2017
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.
#include<stdio.h>
#include<stdlib.h>
int main( int c, char *argv[])
{
printf ( "By the time you run me "
"I will be destroyed \n" );
remove (argv[0]);
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...