Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Anvi Jain

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

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.