A computer virus is a computer program that can copy itself and infect a computer. The term "virus" is also commonly but erroneously used to refer to other types of malware, including but not limited to adware and spyware programs that do not have the reproductive ability. A true virus can spread from one computer to another (in some form of executable code) when its host is taken to the target computer; for instance because
a user sent it over a network or the Internet, or carried it on a removable medium such as a floppy disk,
CD, DVD, or USB drive.
In order to replicate itself, a virus must be permitted to execute code and write to memory. For this reason,
many viruses attach themselves to executable files that may be part of legitimate programs. If a user
attempts to launch an infected program, the virus' code may be executed simultaneously.
Viruses can be divided into two types based on their behavior when they are executed. Nonresident viruses immediately search for other hosts that can be infected, infect those targets, and finally transfer control to the application program they infected. Resident viruses do not search for hosts when they are started. Instead, a resident virus loads itself into memory on execution and transfers control to the host program.
The virus stays active in the background and infects new hosts when those files are accessed by other
programs or the operating system itself.