吉兆什么意思| 立加羽读什么| 腹腔气体多是什么原因| 肛瘘不治疗有什么后果| 艾滋病潜伏期有什么症状| 例假推迟是什么原因| 口干是什么病| 尿毒症是什么原因引起的| 老妈子是什么意思| 尿酸高可以吃什么鱼| 广州有什么特产必带| 一路长虹什么意思| 小孩拉肚子应该吃什么食物好| 东南方是什么生肖| 冰酒是什么酒| 珞字五行属什么| 背上有痣代表什么| 玉米是什么植物| 尿素偏高是什么原因| 过誉是什么意思| 九月初三是什么星座| 梦见好多羊是什么意思| 肝火旺是什么原因引起的| 吃惊的近义词是什么| 丘疹性荨麻疹用什么药| 补血吃什么水果| 办理出院手续都需要什么| 筋膜炎有什么症状| 腰酸痛挂什么科| 不假思索的假是什么意思| 月经不调吃什么调理| 什么是赤道| 小孩掉头发是什么原因| 赤小豆是什么| 植物神经功能紊乱吃什么药最好| 什么时间泡脚最好| dha有什么作用与功效| 拉肚子可以吃什么| 广西属于什么气候| 孕妇梦到老公出轨什么意思| 呼吸道感染吃什么药最好| 肛门周围痒是什么病| 顾名思义的顾什么意思| 孕妇适合喝什么牛奶| 鸡蛋和什么不能一起吃| 十月十七是什么星座| 月经咖啡色是什么原因| 小孩不吃肉是什么原因| 甘油三酯偏高吃什么药| 什么样的人| 鲻鱼是什么鱼| 乙木的根是什么| moschino是什么品牌| 降尿酸吃什么药| 国帑是什么意思| 什么是良心| 小肠气是什么症状| 阎维文什么军衔| 洗发水和洗发露有什么区别| 太阳里面有什么| 赫依病是什么病| 吃什么药能冲开宫腔粘连| 糖尿病喝什么茶| 打酱油是什么意思| 洧是什么意思| 不屑一顾的意思是什么| 什么的坐着| 62年属什么生肖| 湖蓝色是什么颜色| 寿司醋可以用什么代替| 绞股蓝有什么功效| 差强人意什么意思| 天煞孤星是什么意思| 牙齿遇冷热都痛是什么原因| 素女是什么意思| 高筋面粉和低筋面粉有什么区别| 东北大拉皮是什么做的| 脚麻挂什么科| 什么是沙发发质| 吃苹果有什么好处| 硒酵母胶囊对甲状腺的作用是什么| 开放式耳机是什么意思| 呼吸困难气短是什么原因| 尿蛋白1十是什么意思| 益生菌什么牌子最好| 为什么不能叫醒梦游的人| 肝右叶低密度灶是什么意思| 阴虱用什么药可以根除| 低压高有什么危险| 活学活用是什么意思| maby什么意思| 什么是引产| 鸡蛋为什么这么便宜| 翻来覆去的覆什么意思| 下雨天适合穿什么衣服| 拿什么拯救你我的爱人演员表| 这是什么车| fizz是什么意思| 目加此念什么| 梦见摘桃子是什么意思| 缺钙查什么化验项目| 隔空是什么意思| 老舍原名是什么| 中耳炎吃什么药最有效| 文昌星是什么意思| 二十三岁属什么生肖| 鸡是什么类| 又当又立是什么意思| 甲亢看什么科| 产前筛查是检查什么| 北京市副市长是什么级别| 栀子有什么功效| 正师级是什么军衔| 男生为什么喜欢女生叫爸爸| 病字旁加且念什么| 靖五行属性是什么| 整装待发是什么意思| 手指麻木什么原因| 氨气是什么| 白炽灯属于什么光源| e m s是什么快递| 日本天皇姓什么| 中元节注意什么| 阴虱长什么样| 向日葵为什么会随着太阳转动| 险象环生是什么意思| 什么品牌的笔记本好| 休息是什么意思| 世界第八大奇迹是什么| 菠萝蜜不能和什么一起吃| 亚麻籽油有什么功效| 心电图诊断窦性心律什么意思| 号是什么| 即视感是什么意思| 许三多最后是什么军衔| 肛门上长了个肉疙瘩是什么原因| 抗体和抗原有什么区别| 缺钙吃什么补钙最快| 什么体质容易长结石| 抚琴是什么意思| 朱元璋是什么朝代| 风寒感冒吃什么药好| 人情味是什么意思| 什么前什么后| 十二月二号是什么星座| 吵架是什么意思| 头疼是什么原因引起| 四大美女是什么生肖| 草龟吃什么蔬菜| 沈腾和马丽是什么关系| 真太阳时是什么意思| 正月初六是什么星座| 嗓子发苦是什么原因| hiit是什么意思| 实性结节是什么意思| 向日葵什么时候成熟| 乌鸡汤放什么补气补血| 用什么刷牙能使牙齿变白| 13是什么| 鼻炎是什么原因引起的| 黄瓜籽有什么功效| 怀孕胎盘低有什么影响| 陈小春什么星座| cosplay什么意思| 喝什么饮料解酒最快| hpm是什么意思| 长痘要忌口什么东西| 报销什么意思| d二聚体是查什么的| 娥皇女英是什么意思| 左行气右行血什么意思| 听佛歌有什么好处| vintage什么意思| 海马吃什么| 观落阴是什么意思| 58年属狗是什么命| 属马的男生和什么属相最配| 高育良什么级别| 浇去掉三点水读什么| 什么津津| 什么叫散光| 什么时候做nt| ims是什么意思| 男生做爱什么感觉| 被什么虫子咬了会刺痛| 空调数显是什么意思| 嘴唇干裂脱皮是什么原因| 什么是商k| 拉黑粑粑是什么原因啊| 吃什么促进恶露排干净| 兑卦五行属什么| fk是什么意思| 6月16号是什么星座| 尿分叉是什么原因引起的| 紫罗兰是什么颜色| 堃什么意思| 病毒感染吃什么消炎药| 辐照食品是什么意思| 坐西向东是什么宅| 蓝莓什么时候吃最好| pp是什么意思| 吃什么补维生素d| 胃肠湿热吃什么中成药| 什么是华盖| 红玛瑙适合什么人戴| 内向的人适合什么工作| 微信中抱拳是什么意思| 雪里红是什么菜| 乙肝两对半定量是什么意思| 耳朵里发炎用什么药好| 排卵期和排卵日有什么区别| 花胶有什么功效| 倒拔垂杨柳是什么意思| 燕窝补什么| 一什么野菜| clean什么意思| utc是什么意思| 抗核抗体阳性对怀孕有什么影响| 转音是什么意思| 白鸭是什么鸭| 老是想睡觉是什么原因| 番薯是什么时候传入中国的| 熊猫属于什么科| 山西为什么叫山西| 螨虫用什么药膏| 地藏菩萨是管什么的| 骨头受伤了吃什么恢复的快| 心率过快是什么原因| 什么药补血最快| 硝化细菌是什么| 蝴蝶骨是什么| 胸闷气短挂什么科室| 白痰多是什么原因造成的| 百合什么时候种| 吃什么养肾| 乙肝表面抗体定量偏高什么意思| 琪五行属什么| 心衰竭是什么病严重吗| 为什么腿会抽筋| 梁伟文 为什么叫林夕| 一一是什么意思| 射精是什么| 尿hcg阴性是什么意思| 尿检隐血弱阳性是什么意思| 妇科千金片和三金片有什么区别| 夜不能寐是什么意思| 忌出行是什么意思| 盆腔炎用什么药好| 三点水一个兆读什么| 双侧筛窦粘膜增厚是什么意思| 经期为什么不能拔牙| 女人吃玛卡有什么好处| 支气管舒张试验阳性说明什么| 尤加一笔是什么字| 胃发炎吃什么药好得快| 血氧是什么意思| 心心相惜是什么意思| 女人叫床最好喊什么| 肌肉一跳一跳什么原因| 什么是低密度脂蛋白| 郑板桥擅长画什么| 心脑供血不足吃什么药效果最好| domyos是什么牌子| 缩阳什么意思| 百度Jump to content

《小小农场主》预计将于今年5月25日正式上线

From Wikipedia, the free encyclopedia
百度 (责编:龚霏菲、王珩)

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-07. Retrieved 2025-08-07.
  7. ^ How Not To Pick Variables Archived 2025-08-07 at the Wayback Machine, Retrieved July 11, 2012 [DEAD LINK]
  8. ^ Edsger Dijkstra, To hell with "meaningful identifiers"!

Works cited

[edit]
刷存在感是什么意思 什么水果含糖量最低 36岁属什么生肖 心跳过缓是什么原因造成的 肠胃出血有什么症状
什么不安成语 对等是什么意思 玻璃的原材料是什么 弟弟的女儿叫什么 6月初三是什么日子
阴雨连绵是什么意思 渐冻症是什么 12583是什么电话 乐高为什么这么贵 乳酸杆菌大量是什么意思
燕麦片热量高为什么还能减肥 饭后胃胀是什么原因导致的 fnc是什么意思 右腹疼是什么原因 湖北人喜欢吃什么菜
巡礼是什么意思hcv7jop5ns2r.cn 蒂是什么意思hcv8jop4ns1r.cn 丁字是什么意思hcv9jop2ns2r.cn 免贵姓是什么意思hcv8jop1ns2r.cn 左手有点麻是什么原因hcv8jop1ns4r.cn
碘缺乏会导致什么疾病hcv7jop7ns2r.cn 六月二十五号是什么星座hcv9jop6ns3r.cn 贫血是什么意思hcv8jop9ns6r.cn 什么叫直男hcv8jop9ns1r.cn 放生是什么意思hcv9jop0ns9r.cn
做月子吃什么菜hcv9jop4ns8r.cn 什么叫四维空间hcv8jop0ns7r.cn 吃了虾不能吃什么yanzhenzixun.com 什么是玉石hcv7jop9ns3r.cn 扁桃体发炎吃什么消炎药hcv8jop1ns7r.cn
狗狗打喷嚏流鼻涕怎么办吃什么药hcv7jop4ns5r.cn 梦见做鞋子是什么意思hcv8jop0ns4r.cn 妈祖是什么意思96micro.com 扁桃体作用是什么hcv7jop9ns1r.cn 双侧骶髂关节致密性骨炎是什么病hcv9jop7ns3r.cn
百度