Self Destructing Code in C


Here we will see how to create self-destructing code in C. The self-destructing code is basically executing the code, and then remove the executable file after executing it.

This task is very simple. We need to get the executable file name to remove it. We can use the command line arguments. The argv[0] will hold the executable filename. Then using remove() function we can remove it.

In the program we can see that one line is printing after the removing of that file. So now question comes how the next line is executing while current file is not present?

Actually the entire converted code is copied into primary memory before executing it. The executing file content is copied; it is not used itself. So from the primary memory, the next line will be printed.

Example

#include<stdio.h>
int main(int c, char *argv[]) {
   printf("After completing this, the file will be removed\n");
   remove(argv[0]); //remove the argv[0] this is the name of the executable
   printf("Removed\n");
   return 0;
}

Output

After completing this, the file will be removed
Removed

Updated on: 30-Jul-2019

944 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements