晚黄瓜什么时候种| 龙象征着什么| 手臂肌肉跳动是什么原因| 虎的贵人是什么生肖| 阴茎不硬吃什么药| 面部痉挛吃什么药| 孕妇梦见摘桃子是什么意思| 什么是乙肝病毒携带者| 射手男喜欢什么样的女生| 什么是耐药性| 什么是cpi| 陕西有什么特产| 什么办法退烧快| 灵隐寺求什么最灵验| 老生常谈是什么意思| 远视眼是什么意思| 糖化血红蛋白是什么意思| 怀孕有什么现象| ngu是什么意思| 一五行属什么| 尿红色是什么原因| 胃粘膜糜烂吃什么药| 胸片能查出什么| vivi是什么意思| 晨勃是什么意思啊| 炫的意思是什么| 喉咙干痒咳嗽吃什么药| 1月29号什么星座| 脊髓空洞症是什么病| a4腰什么意思| 4.9是什么星座| 有结石不能吃什么东西| 五月十八什么星座| 胃溃疡吃什么药好得快| 相公是什么意思| 越南有什么特产| 澳大利亚有什么特产| 河南有什么大学| 外周动脉僵硬度增高什么意思| 昔字五行属什么| 阿托伐他汀钙片什么时候吃最好| 肠胃不好喝什么茶| 中山市有什么大学| 送行是什么意思| 温州人为什么会做生意| 象牙白适合什么肤色| 正直是什么意思| ket是什么意思| 什么节吃饺子| sars是什么病毒| 肾挂什么科| 儿童用什么洗发水好| bn是什么颜色| 什么为笑| 怀孕有什么征兆| 滚床单是什么意思| rsv是什么病毒| 头颈出汗多是什么原因| 乙肝核心抗体高是什么意思| 为什么会牙疼| 社恐的人适合什么工作| 三氯蔗糖是什么东西| 雨后的彩虹像什么| 平菇炒什么好吃| 秦始皇为什么要焚书坑儒| 四岁属什么生肖| 右眼皮跳什么原因| 鸭肉不能和什么一起吃| 炙子是什么意思| 桥本甲状腺炎是什么意思| 铁剂是什么| 手上为什么长湿疹| 白细胞偏低是什么原因| 夕阳无限好是什么意思| 青提是什么| 7月15日是什么日子| 一什么水井| 同病相怜是什么意思| few是什么意思| 绮字五行属什么| 三月初什么星座| 整个后背疼是什么原因| 30如狼40如虎是什么意思| 脸容易红是什么原因| 尿有泡泡是什么病| 唇珠在面相中代表什么| 什么地哭| ct值是什么意思| 仙女下凡是什么生肖| aq是什么标准| 什么是肠息肉| 孕反什么时候结束| 感冒什么时候传染性最强| 右眼皮跳是什么预兆女| 吃银耳有什么好处和坏处| 八月初十是什么星座| max是什么意思| 腮腺炎是什么引起的| 站姐是什么职业| 左眼屈光不正是什么意思| 怀孕前壁和后壁有什么区别| 空腹打嗝是什么原因引起的| vans属于什么档次| 宫保鸡丁是什么菜系| 老年人吃什么钙片补钙好| 水豚吃什么| 黑加仑是什么水果| 手肿脚肿是什么原因引起的| 失眠吃什么食物效果最好| 入职体检70元一般检查什么| 绝经三年了突然又出血了什么原因| 来事头疼什么原因| 喝什么茶养肝护肝| 眼肿是什么原因引起的| 丝瓜配什么炒好吃| 北芪与黄芪有什么区别| 梦特娇属于什么档次| 做美甲师容易得什么病| 心跳过速吃什么药| 2010年什么年| 肩周炎口服什么药最好| 体重kg是什么意思| 什么叫引流| 什么什么不安| 如果你是什么就什么造句| 虾腹部的黑线是什么| 痛风不能吃什么东西| 烤冷面是什么材料做的| 女团ace是什么意思| 立秋是什么意思| 筑基是什么意思| 低置胎盘有什么危险| spiderking是什么牌子| 什么食物补血效果最好最快| 碍事是什么意思| 故的偏旁是什么| 爆菊花是什么意思| 籍贯一般写什么| 莴笋炒什么好吃| 牙髓炎吃什么药最有效| 精子发黄是什么原因| 监狱长是什么级别| 酒是什么| 3月2日是什么星座| 二月出生是什么星座| 1976年是什么命| t和p是什么意思| 癸是什么意思| 熊人是什么意思| 小宝贝是什么意思| 氯低是什么原因| 肺炎支原体抗体阴性是什么意思| 什么洗发水好| 燃面为什么叫燃面| 台湾什么时候收回| 红细胞偏高有什么危害| 艾滋通过什么途径传播| 流产后不能吃什么东西| 孜然是什么植物| 疣长什么样| 失眠吃什么食物效果最好| 夜郎自大是什么意思| 脑供血不足吃什么| 孩子打呼噜是什么原因| 午字五行属什么| 甲状腺肿大吃什么药| 耍宝是什么意思| 初一不能做什么| 体型最大的恐龙是什么| 七月四号是什么星座| 青头鸭和什么煲汤最好| 被子什么材质的好| 男生喜欢什么样的女生| 腋窝痒是什么原因| 腋下臭是什么原因| 五一年属什么生肖| 什么叫丹凤眼| 理疗师是做什么的| 舌头涩是什么原因| 等闲变却故人心却道故人心易变什么意思| 精神支柱是什么意思| 有什么好处| 绿色的鸟是什么鸟| 洗钱是什么意思| 脸上长肉疙瘩是什么原因| 长白头发了吃什么才能把头发变黑| 香仪是什么意思| 四平八稳是什么生肖| 难缠是什么意思| 天明是什么意思| 什么家庭不宜挂八骏图| 什么行业赚钱| 排骨炒什么配菜好吃| 什么是肾阴虚和肾阳虚| 石楠花是什么| 晚上吃什么能减肥| 幽默什么意思| 产妇吃什么水果| 草酸是什么| earth是什么意思| 困觉是什么意思| md是什么牌子| 一桌餐是什么意思| 轻微脑梗吃什么药| 尿毒清颗粒主治什么病| 肺结核吃什么食物好| 什么学习机好| 什么情况下需要打破伤风| 胃窦小弯是什么意思| 天麻泡水喝有什么功效| 3月3日什么星座| 颈椎痛挂什么科| 亚麻是什么| 卵泡排出来是什么样的| 嘌呤是什么意思| 世界上最小的动物是什么| 晚上尿多是什么原因| 红花油和活络油有什么区别| 一什么露珠| 胃有灼烧感是什么原因| 吃什么补红细胞最快| 翡翠和和田玉有什么区别| 脚心有痣代表什么意思| 肌肉抽筋是什么原因| 运动后想吐是什么原因| 信徒什么意思| 移民澳洲需要什么条件| 吃什么有助于骨头愈合| 白带变绿用什么药| 什么可以补肾壮阳| 胆固醇低是什么原因| 世袭罔替是什么意思| 为什么不嫁丧妻之男| 吃什么降胆固醇| 心脏缺血吃什么补得快| 巨蟹跟什么星座最配| 甘露醇治什么病| 甘油三酯高是指什么| 喉炎是什么原因引起的| 中药和中成药有什么区别| 三个女是什么字| 放疗和化疗有什么区别| 起鸡皮疙瘩是什么原因| 调和营卫是什么意思| 无人区是什么意思| 1958年属什么| 党委副书记是什么级别| 武则天什么星座| 生长激素是什么| prada是什么牌子| 克星是什么意思| 难以启齿什么意思| 一什么一什么| 女人喝咖啡有什么好处和坏处| 青枝骨折属于什么骨折| 伏特加是什么酒| 叛逆是什么意思| 什么叫靶向药| 女人耳垂大厚代表什么| 为什么老是出汗| rangerover是什么车| 舌头溃疡吃什么药最好| 脸上长癣是什么原因| 氧氟沙星和诺氟沙星有什么区别| 百度Jump to content

《死侍2》发布首款正式预告 死侍电索大打出手

From Wikipedia, the free encyclopedia
百度 桃江县人民政府则发文称,力争再通过2个多月的治疗,达到高考体检标准。

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]
曹操的脸谱是什么颜色 penguin是什么意思 法西斯战争是什么意思 恩施玉露是什么茶 加盟什么品牌好
全血检查能查些什么病 拥趸是什么意思 脾胃虚寒吃什么食物好 西西里的美丽传说讲的什么 12点到1点是什么时辰
地黄长什么样 什么手什么足 朋友开业送什么礼物好 什么情 打完除皱针注意事项有什么
什么症状提示月经马上要来了 男性泌尿道感染吃什么药 纵隔子宫是什么意思 相亲是什么意思 吃什么有利于排便
轻医美是什么hcv8jop9ns4r.cn 天葬是什么hcv8jop7ns4r.cn 防弹衣为什么能防弹hcv9jop1ns3r.cn 什么是结肠炎hcv9jop7ns0r.cn 脚趾麻木是什么病先兆hcv9jop4ns9r.cn
同房为什么会出血mmeoe.com 生日礼物送什么hcv8jop7ns9r.cn 象是什么结构的字hcv7jop6ns9r.cn 手腕关节疼痛什么原因引起的hcv9jop0ns9r.cn 身上遇热就痒是什么病hcv7jop9ns3r.cn
客套是什么意思hcv9jop6ns0r.cn 什么是翡翠hcv8jop4ns5r.cn 肾虚有什么症状hcv9jop0ns3r.cn 鼻子上长脓包型痘痘是什么原因hcv7jop7ns3r.cn 男人梦到掉牙什么预兆hcv9jop5ns6r.cn
身份证号码最后一位代表什么hcv9jop3ns4r.cn 方脸适合什么发型hcv9jop1ns7r.cn 下一年是什么生肖hcv7jop9ns8r.cn 小儿抽搐是什么原因引起的hcv9jop0ns8r.cn 鸡冲什么生肖travellingsim.com
百度