
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- Self Crossing in C++
- Managed code vs Unmanaged code in C#
- Count of Smaller Numbers After Self in C++
- Gray Code in C++
- self in Python class
- Executing C# code in Linux
- Writing OS Independent Code in C/C++
- Self–invoking function in JavaScript?
- Self-Regulation in Health Behaviour
- The Cognitive Self
- The Social Self
- What is unmanaged code in C#?
- C++ Program to Implement self Balancing Binary Search Tree
- C++ Program to Perform Searching Using Self-Organizing Lists
- Writing C/C++ code efficiently in Competitive programming
