
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Self Destructing Code in C
The self-destructing code is a type of program that can delete itself. It automatically executes the program and then removes the executable file once the execution is done. In this article, we will learn how to write self-destructing code for a C program.
Write a Self Destructing Code in C++
You can write a self-destructing program that deletes its own executable file after it finishes running by using the remove() function. The remove() function is a built-in function from the C standard library (<stdio.h>) that deletes the specified file from the file system.
To delete the program's own executable file, you need to pass argv[0] to the remove() function. The argv[0] contains the name or path of the executable file.
Thus, the statement remove(argv[0]) tells to the compiler to delete the itself i.e., the executable file of the current program.
C++ Program for Writing Self-destructing Code
Below is a C++ program demonstrating the self-destructing code:
#include<stdio.h> #include<stdlib.h> int main(int argc, char *argv[]) { printf("Running self-destruct sequence...\n"); // argv[0] holds the path to the executable // delete the executable file itself if (remove(argv[0]) == 0) { // if successful, print the message printf("Executable deleted successfully.\n"); } else { // if error occurs, print the error perror("Error deleting the file"); } return 0; }
The above code produces the following result:
Running self-destruct sequence... Executable deleted successfully.