女人的第二张脸是什么| 骶椎腰化什么意思| 大便颗粒状是什么原因| 心慌气短是什么原因| 反流性食管炎可以吃什么水果| 腿抽筋吃什么钙片好| 清和是什么意思| 紫色睡莲的花语是什么| ercp是什么检查| 木色是什么颜色| 扁桃体肥大有什么影响| 啰嗦是什么意思| 大象是什么颜色| 牙龈肿是什么原因引起的| apl是什么意思| 心脏看什么科室| 固摄是什么意思| 为什么叫关东军| 雨中漫步是什么意思| 三月初六是什么星座| 条子是什么意思| 带环了月经推迟不来什么原因| 仙人掌有什么作用| 潮汐车道什么意思| 蓝字五行属什么| 让平是什么意思| 谷氨酰转肽酶高是什么原因| 桃子吃了有什么好处| 尪痹片主治什么| 咳嗽挂什么科| 芒果和什么榨汁好喝| 曙光什么意思| 我是小姨的什么人| 淡奶油是什么| 女人脸色发黄吃什么补得最快| 发量多的女生适合什么发型| 边际贡献是什么意思| 甜菜根是什么菜| 脸书是什么意思| 经期吃什么好| 随性什么意思| 发烧适合吃什么水果| 女人什么眉毛最有福气| 维字五行属什么| 什么是伴手礼| 脚疼是什么原因引起的| 治脚气用什么药| 身心是什么意思| 今天突然拉稀拉出血什么原因| 呕吐是什么原因引起的| 二级烧伤是什么程度| 一学年是什么意思| 什么的交流| 五黄煞是什么意思| 臀疗是什么| 什么白云| 做梦怀孕了是什么意思| 十二生肖叫老大是什么生肖| 隐翅虫皮炎用什么药| 吃韭菜有什么好处和坏处| 陈皮有什么作用| 挂是什么意思| 泌尿外科主要看什么病| 白蛋白高是什么原因| 扁桃体发炎咳嗽吃什么药效果好| 血糖高可以喝什么粥| 早上手肿胀是什么原因| 7月26日是什么日子| 大名鼎鼎是什么意思| 舌头有裂痕是什么原因| 三个土字念什么字| 相是什么生肖| mm是什么意思| 三月是什么生肖| 为什么耳朵后面会臭| 晚上睡觉脚底发热是什么原因| 胎盘植入是什么意思| 恩惠是什么意思| 嗓子干吃什么药| 蔡明是什么民族| 灵芝孢子粉有什么用| 脑宁又叫什么名字| 自然生化流产是什么意思| 7.7是什么星座| jones是什么意思| 定位是什么意思| 欲盖弥彰是什么意思| peak是什么牌子| 九月份有什么节日| 风属于五行属什么| 肉蒲团是什么意思| 悦风空调是什么牌子| 钙对人体有什么作用| 面瘫什么意思| 睾丸疼痛挂什么科| 女人经常喝什么汤养颜| 为什么会得牙周炎| 备孕要吃什么| 豚鼠吃什么食物| 农历正月是什么星座| 甲钴胺不能和什么药一起服用| 气血不足吃什么中成药最好| 中暑吃什么好| 皮肤干燥是什么原因| 什么是宫外孕| 鱼缸摆放什么位置最佳| 左室舒张功能减低是什么意思| 五个月宝宝可以吃什么水果| 瞑眩反应是什么意思| 什么叫捞女| 肩胛骨缝疼挂什么科| 吹箫是什么意思| 乡长是什么级别| 晚上磨牙是什么原因| 梦见猫死了是什么意思| 糖尿病人吃什么水果好| a1代表什么意思| 心肾不交失眠吃什么中成药| 腿上长水泡是什么原因引起的| 脚肿什么病| 什么食物好消化| 右眼袋跳动是什么原因| 弓箭是什么时候发明的| 居高临下是什么意思| 吃什么补白细胞效果最好| hcd是什么意思| 夜盲症是什么| 医助是什么工作| 儿童胃炎吃什么药| 脆肉鲩是什么鱼| 幽门杆菌吃什么药最好| 小肝癌是什么意思| 为什么会早泄| 统筹支付是什么意思| 肠胃炎不能吃什么| 隐翅虫长什么样子| 遮羞布是什么意思| 肚脐眼左边是什么部位| 锻练与锻炼有什么区别| 后脑两侧痛是什么原因| 胃为什么会疼| 什么东西可以淡化疤痕| lh是什么意思啊| 芈姝结局是什么| 睡眠不好吃什么中成药| 浓鼻涕吃什么药| 皮肤痒有什么特效药| 什么样的天山| 车间管理人员工资计入什么科目| 平片是什么| 翌日什么意思| 绝育手术对女性有什么危害| 雪芽是什么| 中性粒细胞百分比高是什么原因| 羊的守护神是什么菩萨| 鸭胗是鸭的什么部位| 绿豆什么人不能吃| 九浅一深什么意思| 大便变黑是什么原因| 喉咙痛喝什么饮料| 11月21是什么星座| 日后好相见的前一句是什么| 好马不吃回头草什么意思| 小蓝是什么| upupup是什么意思| 京剧脸谱黑色代表什么| 君子兰不开花是什么原因| 双子座上升星座是什么| 安全三原则是指什么| 忌讳是什么意思| 炖排骨什么时候放盐最好| 湿疹和热疹有什么区别| 蜂蜜水喝了有什么好处| 所向披靡是什么意思| 随性什么意思| 早射吃什么药可以调理| 济南有什么景点| 11月份是什么星座| 什么的什么是什么的伞| 死心眼什么意思| 部堂大人是什么职位| 肾炎是什么症状| 一朝一夕是什么意思| 西班牙用的是什么货币| 请人帮忙用什么词| 蜂蜜加柠檬有什么功效和作用| 山合念什么| 你喜欢什么动物| 什么的山野| 肾与性功能有什么关系| 晚上睡觉小腿抽筋是什么原因| 4.4是什么星座| 心慌挂什么科| 上皮细胞一个加号什么意思| 阴囊湿疹用什么药膏效果最好| 颈椎病看什么科| 为什么体重一直下降| 嗓子发炎肿痛吃什么药| 忧心忡忡是什么意思| 慢性非萎缩性胃炎吃什么药效果好| 手指甲软薄是缺什么| 肾的主要功能是什么| 男人梦到掉牙什么预兆| 鼻子经常出血是什么病征兆| 梦见买馒头是什么意思| 蒸鱼豉油什么时候放| 嘌呤是什么东西| 女人右眼跳是什么意思| 什么什么的太阳| 人发胖的原因是什么引起的| 上不来气吃什么药| 黄芪精适合什么人喝| 什么是鬼压床| 什么药止血效果最快| 静脉曲张做什么检查| 牛河是什么| 什么是三伏贴| 朋友开业送什么礼物好| 利福喷丁和利福平有什么区别| 龙的九个儿子都叫什么名字| 富贵竹开花什么预兆| 冰箱发烫是什么原因| 爱情的故事分分合合是什么歌| 吃生姜对身体有什么好处和坏处| 虾不能和什么水果一起吃| 湖北九头鸟是什么意思| 昆仑雪菊有什么功效| 恍然大悟什么意思| 宫腔粘连是什么原因引起的| fujixerox是什么牌子| 什么情况下做胃镜| 吃什么水果减肥| 经常尿路感染是什么原因| 梦见纸钱是什么预兆| 什么样的镜子| 网线是什么| 西海龙王叫什么| 空调什么牌子的好| 抗ro52抗体阳性是什么意思| 为什么手脚老是出汗| 心火旺吃什么药| 羊经后半边读什么| 一物降一物指什么生肖| 还人是什么意思| 什么手机信号最好最强| 什么是ppi| 天下乌鸦一般黑是什么意思| 宝宝不喝奶是什么原因| 香蕉皮擦脸有什么作用与功效| 做什么梦暗示你怀孕了| 北京豆汁儿什么味道| 女人吃什么补充雌激素| 栀子花叶子发黄是什么原因| 肛裂是什么| 痛风什么东西不可以吃| 条子是什么意思| 7代表什么意思| 胆小怕事是什么生肖| 中性粒细胞百分比偏低什么意思| 什么叫雷达| 间谍是什么意思| 肾钙化是什么意思| 吃芒果对人有什么好处| 579是什么意思| 百度Jump to content

志趣相投是什么意思

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

Works cited

[edit]
五点多是什么时辰 金开什么字 甙是什么意思 人工智能是什么意思 中天是什么意思
宫颈糜烂什么症状 三点水一个金读什么 不放屁是什么原因 25周岁属什么生肖 什么泡面最好吃
哈儿是什么意思 流产期间吃什么好 梦见火车脱轨什么预兆 恋爱脑什么意思 胎监是检查什么的
吃什么补白细胞效果最好 唐僧真名叫什么 ips屏幕是什么意思 减肥吃什么主食 锌中毒是什么症状
夺嫡是什么意思onlinewuye.com 雪藏是什么意思wuhaiwuya.com 永五行属什么hcv9jop6ns2r.cn 戒奶涨奶痛有什么缓解方法hcv8jop2ns8r.cn 6月16日是什么星座zhongyiyatai.com
补血最快的方法是什么hcv8jop4ns3r.cn 过敏应该挂什么科hcv9jop0ns7r.cn 心什么胆什么hcv8jop3ns8r.cn 尿酸高说明什么问题hcv8jop0ns2r.cn 1937年是什么年0735v.com
跃跃欲试什么意思hcv9jop6ns1r.cn 什么的国王hcv8jop1ns0r.cn 气血不足看什么科室hcv8jop9ns4r.cn 名人轶事是什么意思0297y7.com 鸡肉和什么菜搭配最好jasonfriends.com
肾寒吃什么中成药gysmod.com 气郁症是什么症状hcv8jop6ns0r.cn 生物素是什么hcv8jop1ns0r.cn 荼什么意思hcv8jop8ns6r.cn 脾胃不好有什么症状表现qingzhougame.com
百度