# Temporary variable

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Temporary_variable
> Markdown URL: https://mediated.wiki/source/Temporary_variable.md
> Source: https://en.wikipedia.org/wiki/Temporary_variable
> Source revision: 1345463805
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{more citations needed|date=November 2025}}
In [computer programming](/source/computer_programming), a '''temporary variable''' is a [variable](/source/variable_(programming)) with short [lifetime](/source/object_lifetime),<ref>{{Cite book |last1=Gafoor |first1=Rafael |title=A Comprehensive Guide to Coding and Programming in Stata |chapter=Temporary Names, Variables and Files |date=2024 |pages=6–10 |publisher=Taylor & Francis |language=en |doi=10.1201/9781003483779-2 |isbn=978-1-003-48377-9}}</ref><ref>{{cite journal | last1=Ng | first1=Kelvin S. | last2=Leckebusch | first2=Gregor C. | title=A new view on the risk of typhoon occurrence in the western North Pacific | journal=Natural Hazards and Earth System Sciences | date=2021 | volume=21 | issue=2 | pages=663–682 | doi=10.5194/nhess-21-663-2021 | doi-access=free | bibcode=2021NHESS..21..663N }}</ref> usually to hold [data](/source/data_(computing)) that will soon be discarded, or before it can be placed at a more permanent [memory location](/source/memory_location). Because it is short-lived, it is usually declared as a [local variable](/source/local_variable), i.e., a variable with local [scope](/source/scope_(computer_science)). There is no formal definition of what makes a variable ''temporary'', but it is an often-used term in programming.

A typical example would be that of [swapping](/source/swap_(computer_programming)) the contents of two variables. Temporary variables, along with [XOR swaps](/source/Exclusive_or) and [arithmetic operators](/source/arithmetic_operators),  are one of three main ways to exchange the contents of two variables. To swap the contents of variables "a" and "b" one would typically use a temporary variable ''temp'' as follows, so as to preserve the data from ''a'' as it is being overwritten by ''b'':

 temp := a := b
 b := temp

Temporary variables are [usually named](/source/Naming_convention_(programming)) with [identifier](/source/identifier)s that abbreviate the word ''temporary'', such as ''temp'', ''tmp'' or simply ''t'', or
with common [metasyntactic variable](/source/metasyntactic_variable) names, the most common of which are ''foo'', ''bar'', ''baz'' (see also [foobar](/source/foobar)).

[Computer hardware](/source/Computer_hardware) is designed to exploit the behaviour of temporary data: a [cache](/source/cache_hierarchy) or [register file](/source/register_file) may contain temporaries internally to a [microprocessor](/source/microprocessor), such that they never need to be committed to [main memory](/source/main_memory) (hence consuming no external [memory bandwidth](/source/memory_bandwidth)).

==See also==
{{Portal|Computer programming}}
* [Temporary folder](/source/Temporary_folder)
* [Temporary file](/source/Temporary_file)
* [Temporary filesystem](/source/Temporary_filesystem)

==References==
{{reflist}}

{{DEFAULTSORT:Temporary Variable}}
Category:Variable (computer science)

---
Adapted from the Wikipedia article [Temporary variable](https://en.wikipedia.org/wiki/Temporary_variable) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Temporary_variable?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
