毛主席什么时候去世的| 焕字五行属什么| 葡萄籽什么牌子效果好| 骨髓穿刺能查出什么病| 京东自营店什么意思| 香瓜什么时候成熟| 7月1日什么节| 蕈是什么意思| 宫颈粘液栓是什么样的| 拔牙之后需要注意什么事项| 复方是什么意思| 呼吸困难是什么原因引起的| 脖子后面正中间有痣代表什么| mch是什么意思| 布丁是用什么做的| 抗体和抗原有什么区别| 抓手是什么意思| oce是什么牌子| 困惑是什么意思| 多囊是什么意思| 虫草适合什么人吃| 坏血病是什么| 零度是什么意思| 藏青色配什么颜色好看| s倾向是什么意思| 身份证复印件是什么| 眼角膜是什么| 睡觉流口水吃什么药| 鬼冢虎什么档次| 山茱萸是什么| 4月1日什么星座| 正常人为什么传导阻滞| ex是什么的缩写| 形影不离是什么意思| 虎的本命佛是什么佛| 喜欢吃酸的是什么原因| 白蛋白偏低是什么意思| 容祖儿老公叫什么名字| 广东有什么烟| 用减一笔是什么字| 什么地散步| 禄位是什么意思| 芝麻吃多了有什么坏处| 毛囊炎用什么药最有效| 女人吃人参有什么好处| 痔疮为什么会出血| 元气大伤什么意思| 慈悲是什么意思| 金银花泡水喝有什么功效| 什么叫主动脉硬化| 爱睡觉是什么原因| 为什么会得湿疹| 取环后要注意什么事项| 桂圆龙眼有什么区别| 龟头有白色污垢是什么| 红楼梦主要讲了什么| 流产能吃什么水果| 空白是什么意思| 牡丹什么时候开花| 超声波检查是什么检查| 高我是什么意思| 作死是什么意思| mlb中文叫什么| 长期尿黄可能是什么病| 肾宝片有什么副作用吗| hpv6阳性是什么意思| 如来佛祖叫什么名字| 欣字属于五行属什么| die是什么意思| 什么药降肌酐最有效| 早博是什么| 红米饭是什么米| 什么是初吻| 脚踝肿是什么病| 女生小便带血是什么原因| 吃槟榔有什么好处和坏处| 为什么说肺结核是穷人病| 痛风能喝什么酒| 甲状腺功能亢进吃什么药| 女人右眼跳是什么预兆| 纣王叫什么名字| 什么药降糖效果最好| 肚脐眼痒是什么原因| 车震什么意思| 酵母是什么| 孩子流黄鼻涕是什么原因| 土的行业有什么工作| 脑梗死是什么意思| 什么是消融手术| hrv是什么病毒| 梦见自己爷爷死了是什么预兆| 汉族为什么叫汉族| 梦见戴帽子是什么预兆| 心脑血管挂什么科| 慢性咽炎吃什么药效果最好| 脾胃不好吃什么药| 3.22什么星座| 苹果跟什么榨汁好喝| 有什么黄色网站| 病理单克隆抗体检测是什么| 避孕套玻尿酸的作用是什么| 秦始皇的佩剑叫什么剑| 满天星的花语是什么| hcg高代表什么| 布谷鸟长什么样| 孩子疱疹性咽峡炎吃什么药| 蝶变是什么意思| 北五行属什么| 小猪佩奇为什么这么火| 一血是什么意思| 防小人应该佩戴什么| 肺和大肠相表里是什么意思| 为什么低血压| it是什么意思| 来月经头晕是什么原因| 索性是什么意思| 墨鱼是什么鱼| 萤火虫为什么会发光| 气短吃什么药效果好| 葡萄糖高是什么原因| 美甲光疗是什么| 咳嗽有黄痰是什么原因| 总做梦是什么原因| 鼻子旁边的痣代表什么| 3月2日什么星座| peaches是什么意思| 年终奖是什么意思| 天秤座女和什么星座最配| 鲨鱼肚是什么| 流感为什么晚上会比白天严重| 秋水长天什么意思| 十月初八是什么星座| 核磁dwi是什么意思| 后背酸痛是什么原因| 回族女人为什么戴头巾| 什么是浸润性乳腺癌| 空调数显是什么意思| 什么时间是排卵期| 世界大同是什么意思| 下身有异味用什么药| 土固念什么| 字母圈什么意思| 花肠是母猪的什么部位| 宫刑是什么意思| 心理活动是什么意思| 姨妈血是黑褐色是什么原因| 糖化血红蛋白是什么| 夏天脚底冰凉是什么原因| 什么夺天工| 心有余悸是什么意思| 尿味道很重是什么原因| 勤字五行属什么| 熊猫为什么吃竹子| 玻璃心是什么意思| 蚕屎有什么作用和功效| 三什么六什么| 乙肝五项45阳性是什么意思| 什么是通勤| 男人嘴小代表什么意思| 52岁属什么| 国家电网是什么编制| 木鱼花是什么做的| 家里停电了打什么电话| 动则气喘是什么原因| 什么是同性恋| 第一次坐飞机需要注意什么| 打鼾是什么原因导致的| 归脾丸的功效与作用治什么病| 什么人适合喝蛋白粉| 姓彭的女孩子取什么名字好| 梦见找鞋子是什么意思| 9月24号是什么星座| 姨妈疼吃什么止疼药| 女人肾虚吃什么药调理| 梦见捡钱是什么意思| 佛性是什么意思| 比热容是什么| 脱靶是什么意思| u盾是什么| 梦见蚯蚓是什么预兆| 儿童过敏性结膜炎用什么眼药水| 肠胃感冒什么症状| 胎芽是什么| 兰台是什么意思| 阎维文什么军衔| 嬴稷和嬴政什么关系| 猪心炖什么补气补血| 合成立方氧化锆是什么| 武则天是什么生肖| 女大七岁有什么说法| vdo是什么牌子| 肩袖损伤吃什么药| 海带补什么| 脑梗会引起什么症状| 速度等于什么| 圣是什么生肖| prada是什么品牌| 胃不好喝什么茶| NG是什么| 鲸属于什么类动物| 骨质增生吃什么药| pvd是什么意思| 坐飞机要带什么证件| 数字是什么意思| 桃子像什么| 粉饼和散粉有什么区别| 子宫肌瘤是什么病严重吗| 感冒反复发烧是什么原因引起的| 狗狗蛋蛋红肿是什么原因| 肚子胀气吃什么药| 1998年五行属什么| 方得始终什么意思| 什么空如洗| 验孕棒一条杠什么意思| 梅花象征着什么| 银饰为什么会变黑| gbs检查是什么| 丝瓜络是什么东西| 输卵管堵塞是什么原因造成的| 手脚发热什么原因| 西安机场叫什么名字| 经常便秘吃什么药好| 嘴角裂口是什么原因怎么办| 纪元是什么意思| ppl是什么药| 正常的月经是什么颜色| 血压下午高是什么原因| 宵夜吃什么| 手臂肌肉跳动是什么原因| 孕妇奶粉什么时候开始喝最好| 什么是行政职务| 肉刺长什么样子图片| 肉桂属于什么茶类| 添堵是什么意思| 血液为什么是红色的| 凝血四项能查出什么病| 绝对值是什么意思| 花椰菜是什么菜| lotus是什么车| 天灵盖是什么意思| 阿迪耐克为什么那么贵| 低密度脂蛋白胆固醇高是什么意思| 手爱出汗是什么原因| 生理期吃什么| 凌霄花什么时候开花| 右眼皮跳代表什么| 尿常规挂什么科| 舂米是什么意思| e抗原阳性是什么意思| 澳大利亚属于什么国家| emba是什么意思| 眼白发青是什么原因| 生长纹是什么| 认贼作父是什么意思| 孕初期需要注意些什么| 什么食物含锌多| 6月12号是什么星座| 纾字五行属什么| 日语斯国一是什么意思| 为什么头出汗特别多| 今日农历是什么日子| 老年性脑改变是什么意思| 口腔溃疡该挂什么科| 彻底是什么意思| 百度

恶作剧是什么意思

百度 西方将俄罗斯视为异类由来已久。

In computer programming, a variable is an abstract storage location paired with an associated symbolic name, which contains some known or unknown quantity of data or object referred to as a value; or in simpler terms, a variable is a named container for a particular set of bits or type of data (like integer, float, string, etc...).[1] A variable can eventually be associated with or identified by a memory address. The variable name is the usual way to reference the stored value, in addition to referring to the variable itself, depending on the context. This separation of name and content allows the name to be used independently of the exact information it represents. The identifier in computer source code can be bound to a value during run time, and the value of the variable may thus change during the course of program execution.[2][3][4][5]

Variables in programming may not directly correspond to the concept of variables in mathematics. The latter is abstract, having no reference to a physical object such as storage location. The value of a computing variable is not necessarily part of an equation or formula as in mathematics. Variables in computer programming are frequently given long names to make them relatively descriptive of their use, whereas variables in mathematics often have terse, one- or two-character names for brevity in transcription and manipulation.

A variable's storage location may be referenced by several different identifiers, a situation known as aliasing. Assigning a value to the variable using one of the identifiers will change the value that can be accessed through the other identifiers.

Compilers have to replace variables' symbolic names with the actual locations of the data. While a variable's name, type, and location often remain fixed, the data stored in the location may be changed during program execution.

Actions on a variable

edit

In imperative programming languages, values can generally be accessed or changed at any time. In pure functional and logic languages, variables are bound to expressions and keep a single value during their entire lifetime due to the requirements of referential transparency. In imperative languages, the same behavior is exhibited by (named) constants (symbolic constants), which are typically contrasted with (normal) variables.

Depending on the type system of a programming language, variables may only be able to store a specified data type (e.g. integer or string). Alternatively, a datatype may be associated only with the current value, allowing a single variable to store anything supported by the programming language. Variables are the containers for storing the values.

Variables and scope:

  • Automatic variables: Each local variable in a function comes into existence only when the function is called, and disappears when the function is exited. Such variables are known as automatic variables.
  • External variables: These are variables that are external to a function and can be accessed by name by any function. These variables remain in existence permanently; rather than appearing and disappearing as functions are called and exited, they retain their values even after the functions that set them have returned.

Identifiers referencing a variable

edit

An identifier referencing a variable can be used to access the variable in order to read out the value, or alter the value, or edit other attributes of the variable, such as access permission, locks, semaphores, etc.

For instance, a variable might be referenced by the identifier "total_count" and the variable can contain the number 1956. If the same variable is referenced by the identifier "r" as well, and if using this identifier "r", the value of the variable is altered to 2009, then reading the value using the identifier "total_count" will yield a result of 2009 and not 1956.

If a variable is only referenced by a single identifier, that identifier can simply be called the name of the variable; otherwise, we can speak of it as one of the names of the variable. For instance, in the previous example the identifier "total_count" is the name of the variable in question, and "r" is another name of the same variable.

Scope and extent

edit

The scope of a variable describes where in a program's text the variable may be used, while the extent (also called lifetime) of a variable describes when in a program's execution the variable has a (meaningful) value. The scope of a variable affects its extent. The scope of a variable is actually a property of the name of the variable, and the extent is a property of the storage location of the variable. These should not be confused with context (also called environment), which is a property of the program, and varies by point in the program's text or execution—see scope: an overview. Further, object lifetime may coincide with variable lifetime, but in many cases is not tied to it.

Scope is an important part of the name resolution of a variable. Most languages define a specific scope for each variable (as well as any other named entity), which may differ within a given program. The scope of a variable is the portion of the program's text for which the variable's name has meaning and for which the variable is said to be "visible". Entrance into that scope typically begins a variable's lifetime (as it comes into context) and exit from that scope typically ends its lifetime (as it goes out of context). For instance, a variable with "lexical scope" is meaningful only within a certain function/subroutine, or more finely within a block of expressions/statements (accordingly with function scope or block scope); this is static resolution, performable at parse-time or compile-time. Alternatively, a variable with dynamic scope is resolved at run-time, based on a global binding stack that depends on the specific control flow. Variables only accessible within a certain functions are termed "local variables". A "global variable", or one with indefinite scope, may be referred to anywhere in the program.

Extent, on the other hand, is a runtime (dynamic) aspect of a variable. Each binding of a variable to a value can have its own extent at runtime. The extent of the binding is the portion of the program's execution time during which the variable continues to refer to the same value or memory location. A running program may enter and leave a given extent many times, as in the case of a closure.

Unless the programming language features garbage collection, a variable whose extent permanently outlasts its scope can result in a memory leak, whereby the memory allocated for the variable can never be freed since the variable which would be used to reference it for deallocation purposes is no longer accessible. However, it can be permissible for a variable binding to extend beyond its scope, as occurs in Lisp closures and C static local variables; when execution passes back into the variable's scope, the variable may once again be used. A variable whose scope begins before its extent does is said to be uninitialized and often has an undefined, arbitrary value if accessed (see wild pointer), since it has yet to be explicitly given a particular value. A variable whose extent ends before its scope may become a dangling pointer and deemed uninitialized once more since its value has been destroyed. Variables described by the previous two cases may be said to be out of extent or unbound. In many languages, it is an error to try to use the value of a variable when it is out of extent. In other languages, doing so may yield unpredictable results. Such a variable may, however, be assigned a new value, which gives it a new extent.

For space efficiency, a memory space needed for a variable may be allocated only when the variable is first used and freed when it is no longer needed. A variable is only needed when it is in scope, thus beginning each variable's lifetime when it enters scope may give space to unused variables. To avoid wasting such space, compilers often warn programmers if a variable is declared but not used.

It is considered good programming practice to make the scope of variables as narrow as feasible so that different parts of a program do not accidentally interact with each other by modifying each other's variables. Doing so also prevents action at a distance. Common techniques for doing so are to have different sections of a program use different name spaces, or to make individual variables "private" through either dynamic variable scoping or lexical variable scoping.

Many programming languages employ a reserved value (often named null or nil) to indicate an invalid or uninitialized variable.

Typing

edit

In statically typed languages such as C, C++, Java or C#, a variable also has a type, meaning that only certain kinds of values can be stored in it. For example, a variable of type "integer" is prohibited from storing text values.[6]

In dynamically typed languages such as Python, a variable's type is inferred by its value, and can change according to its value. In Common Lisp, both situations exist simultaneously: A variable is given a type (if undeclared, it is assumed to be T, the universal supertype) which exists at compile time. Values also have types, which can be checked and queried at runtime.

Typing of variables also allows polymorphisms to be resolved at compile time. However, this is different from the polymorphism used in object-oriented function calls (referred to as virtual functions in C++) which resolves the call based on the value type as opposed to the supertypes the variable is allowed to have.

Variables often store simple data, like integers and literal strings, but some programming languages allow a variable to store values of other datatypes as well. Such languages may also enable functions to be parametric polymorphic. These functions operate like variables to represent data of multiple types. For example, a function named length may determine the length of a list. Such a length function may be parametric polymorphic by including a type variable in its type signature, since the number of elements in the list is independent of the elements' types.

Parameters

edit

The formal parameters (or formal arguments) of functions are also referred to as variables. For instance, in this Python code segment,

>>> def add_two(x):
...     return x + 2
...
>>> add_two(5)
7

the variable named x is a parameter because it is given a value when the function is called. The integer 5 is the argument which gives x its value. In most languages, function parameters have local scope. This specific variable named x can only be referred to within the add_two function (though of course other functions can also have variables called x).

Memory allocation

edit

The specifics of variable allocation and the representation of their values vary widely, both among programming languages and among implementations of a given language. Many language implementations allocate space for local variables, whose extent lasts for a single function call on the call stack, and whose memory is automatically reclaimed when the function returns. More generally, in name binding, the name of a variable is bound to the address of some particular block (contiguous sequence) of bytes in memory, and operations on the variable manipulate that block. Referencing is more common for variables whose values have large or unknown sizes when the code is compiled. Such variables reference the location of the value instead of storing the value itself, which is allocated from a pool of memory called the heap.

Bound variables have values. A value, however, is an abstraction, an idea; in implementation, a value is represented by some data object, which is stored somewhere in computer memory. The program, or the runtime environment, must set aside memory for each data object and, since memory is finite, ensure that this memory is yielded for reuse when the object is no longer needed to represent some variable's value.

Objects allocated from the heap must be reclaimed—especially when the objects are no longer needed. In a garbage-collected language (such as C#, Java, Python, Golang and Lisp), the runtime environment automatically reclaims objects when extant variables can no longer refer to them. In non-garbage-collected languages, such as C, the program (and the programmer) must explicitly allocate memory, and then later free it, to reclaim its memory. Failure to do so leads to memory leaks, in which the heap is depleted as the program runs, risks eventual failure from exhausting available memory.

When a variable refers to a data structure created dynamically, some of its components may be only indirectly accessed through the variable. In such circumstances, garbage collectors (or analogous program features in languages that lack garbage collectors) must deal with a case where only a portion of the memory reachable from the variable needs to be reclaimed.

Naming conventions

edit

Unlike their mathematical counterparts, programming variables and constants commonly take multiple-character names, e.g. COST or total. Single-character names are most commonly used only for auxiliary variables; for instance, i, j, k for array index variables.

Some naming conventions are enforced at the language level as part of the language syntax which involves the format of valid identifiers. In almost all languages, variable names cannot start with a digit (0–9) and cannot contain whitespace characters. Whether or not punctuation marks are permitted in variable names varies from language to language; many languages only permit the underscore ("_") in variable names and forbid all other punctuation. In some programming languages, sigils (symbols or punctuation) are affixed to variable identifiers to indicate the variable's datatype or scope.

Case-sensitivity of variable names also varies between languages and some languages require the use of a certain case in naming certain entities;[note 1] Most modern languages are case-sensitive; some older languages are not. Some languages reserve certain forms of variable names for their own internal use; in many languages, names beginning with two underscores ("__") often fall under this category.

However, beyond the basic restrictions imposed by a language, the naming of variables is largely a matter of style. At the machine code level, variable names are not used, so the exact names chosen do not matter to the computer. Thus names of variables identify them, for the rest they are just a tool for programmers to make programs easier to write and understand. Using poorly chosen variable names can make code more difficult to review than non-descriptive names, so names that are clear are often encouraged.[7][8]

Programmers often create and adhere to code style guidelines that offer guidance on naming variables or impose a precise naming scheme. Shorter names are faster to type but are less descriptive; longer names often make programs easier to read and the purpose of variables easier to understand. However, extreme verbosity in variable names can also lead to less comprehensible code.

Variable types (based on lifetime)

edit

We can classify variables based on their lifetime. The different types of variables are static, stack-dynamic, explicit heap-dynamic, and implicit heap-dynamic. A static variable is also known as global variable, it is bound to a memory cell before execution begins and remains to the same memory cell until termination. A typical example is the static variables in C and C++. A Stack-dynamic variable is known as local variable, which is bound when the declaration statement is executed, and it is deallocated when the procedure returns. The main examples are local variables in C subprograms and Java methods. Explicit Heap-Dynamic variables are nameless (abstract) memory cells that are allocated and deallocated by explicit run-time instructions specified by the programmer. The main examples are dynamic objects in C++ (via new and delete) and all objects in Java. Implicit Heap-Dynamic variables are bound to heap storage only when they are assigned values. Allocation and release occur when values are reassigned to variables. As a result, Implicit heap-dynamic variables have the highest degree of flexibility. The main examples are some variables in JavaScript, PHP and all variables in APL.

See also

edit

Notes

edit
  1. ^ For example, Haskell requires that names of types start with a capital letter.

References

edit
  1. ^ Brookshear 2019, p. 249, "Variables and Data Types", "high-level programming languages allow locations in main memory to be referenced by descriptive names rather than by numeric addresses."
  2. ^ Aho, Alfred V.; Sethi, Ravi; Ullman, Jeffrey D. (1986), Compilers: Principles, Techniques, and Tools, pp. 26–28, Bibcode:1986cptt.book.....A
  3. ^ Knuth, Donald (1997). The Art of Computer Programming. Vol. 1 (3rd ed.). Reading, Massachusetts: Addison-Wesley. pp. 3–4. ISBN 0-201-89683-4.
  4. ^ "Programming with variables". Khan Academy. Retrieved 23 March 2020.
  5. ^ "Scratch for Budding Coders". Harvard. Archived from the original on 23 March 2020. Retrieved 23 March 2020.
  6. ^ "Static typing - MDN Web Docs Glossary: Definitions of Web-related terms | MDN". developer.mozilla.org. 2025-08-06. Retrieved 2025-08-06.
  7. ^ How Not To Pick Variables Archived 2025-08-06 at the Wayback Machine, Retrieved July 11, 2012 [DEAD LINK]
  8. ^ Edsger Dijkstra, To hell with "meaningful identifiers"!

Works cited

edit
海龟汤是什么 鸡冠花什么时候开花 碳酸钠呈什么性 浮屠是什么意思 天梭属于什么档次
梅花什么时候开花 为什么会被鬼压床 什么都有 寿眉属于什么茶 ACEI是什么药
小三阳有什么症状表现 什么时候打仗 脂蛋白磷脂酶a2高说明什么 脑袋痛什么原因 玻璃什么时候传入中国
什么清肠茶好 献完血应该注意什么 网络绿茶是什么意思 脚干脚裂用什么药 胆囊结石不能吃什么
节操是什么意思hcv8jop6ns8r.cn 沙和尚是什么动物变的96micro.com 子宫内膜息肉样增生是什么意思aiwuzhiyu.com 东营有什么大学hcv9jop8ns2r.cn 母仪天下是什么意思hcv8jop3ns8r.cn
瑞士用什么货币hcv8jop9ns5r.cn 玫瑰糠疹是什么原因引起的hcv8jop5ns0r.cn 瞅瞅是什么意思hcv8jop8ns5r.cn 弥漫是什么意思hcv8jop5ns4r.cn 什么样的莲蓬hcv9jop4ns0r.cn
甲胎蛋白什么意思gysmod.com 挚友是指什么的朋友hcv9jop2ns4r.cn 一什么树叶hcv9jop1ns6r.cn 考研复试是什么意思hcv9jop8ns1r.cn 脾的主要功能是什么bfb118.com
植物油是什么hcv7jop4ns6r.cn bone什么意思hcv8jop1ns4r.cn 儿童急性肠胃炎吃什么药hcv9jop6ns2r.cn 女性下面流水什么原因hcv7jop5ns1r.cn 12月2号什么星座onlinewuye.com
百度