Hello, dear programming lovers. I’m glad to see you again in another article of mine. Today we will talk about the most common topic, variable. Many of you probably don’t think about how deep the variables actually are but believe me, this topic is not shallow at all. To better understand them, let’s start.
Many programmers even think that variables consist only of a memory location. However, it can be characterized by 6 attributes, name, address, value, type, lifetime, and scope.
Name — A string of characters used to identify some entity in a program. Some names are common to all programming languages, such as i, j, var1, and so on. Previously, the name of the variable was limited, only 1 character was allowed. Although this limit has been removed in some languages (C # and Java), some programming languages such as C still has up to 31 character restrictions. The first thing we need to consider the names of variables is that they are case-sensitive. Thus, the same names written in uppercase and lowercase letters are actually different variables, such as color and COLOR. Note that there can be no space in the variable name. It is also recommended to use Camel notation when naming variables. The first letter of words other than the first word is capitalized and written together.
Address—A machine address of a variable, also known as l-value (l stands for left because address appears on the left side of an assignment when the name of a variable appears). Think of it as a home. For example, you can find people in their homes. Different variables have different addresses, but let’s not forget that just as two people can live in the same house, different variables can be related to the same address. This is called aliasing in programming. Aliasing reduces readability because by changing the value of one variable, we affect the other. Suppose that the variables a and b point to the same address. By assigning the variable a to 5, we also make the value of variable b equal to 5. I know, it’s a bit confusing.
Value —The contents of the memory cell or cells associated with the variables, also known as r-value (r stands for right because value appears on the right side of an assignment when the name of a variable appears). Cells are imagined to be abstract cells because the value of any variable can take up to 16 bytes of memory depending on the programming language. Computer memories consist of 8-bit units and are called bytes. For example, in C ++, floating-point values may take 4 bytes.
Type — In some programming languages, with the declaration of variables the type is also specified. The type determines position, size of variable memory, the range of values the variable can store, and the set of operations that are defined for values of the type. There are primitive data types, which are built-in and predefined implicitly and can be directly used by the users, derived data types, which are derived from primitive data types, and abstract/user-defined data types, which are defined by the user itself.
Binding is the association between an atribute and entity, such as a variable and its type or a variable and its value. The time the binding happens is called binding time. There are several binding times:
Language design time — The syntax and semantics are typically set here.
Language implementation time — Fixation of implementation contants such as numeric precision, run-time memory sizes.
Program writing time — The time spent for the programmer’s choice of algorithm and data structure.
Compile time — The time of translation of high-level language to machine code.
Link time — The time at which machine code files and libraries are combined into an exetuable file.
Load time — The time at which the operating system loads the executable in the memory
Run-time — The time at which the program executes.
A binding is static if it occurs before run-time begins and remains unchanged thoroughout program execution. A binding is dynamic if it occurs during run-time or can change during the program execution. Static Type Binding can be through implicit declaration, associating variables with the types through default conventions, and explicit declaration, a statement in a program which states a list of all variables. From the point of writeability, it is beneficial, yet it is detrimental to reliability. Dynamic Type Binding requires no declarations and a variable is bound to the type when it’s assigned to a value during assignment. Although this type of binding is flexiable, it is costly and the type error detection by the compiler is difficult.
Lifetime — It is the time between an object’s creation and its destruction.
Categories of variables by lifetime
Static — It’s bound to memory cell before execution
Stack dynamic — Storage binding is created when their declaration is elaborated.
Explicit heap-dynamic — Allocated and Deallocated by explicit directives. It’s specified by the programmer and referenced only through pointers.
Implicit heap-dynamic — Allocation and Deallocation caused by assignment statements. It’s flexible, yet it lacks error detection.
Scope — The range of statements in which the variable is visible to. The variable is visible if it can be references or assigned in that statement. in Static Scoping, we need to find the declarations in the ancestors. Statis scoping is also called lexical scoping. Dynamic Scoping is based on calling sequences of program units. Programs in static-scoping is easier to read and reliable, that’s the reason why dynamic scoping is not used much.
This is the end of the article. Feel free to ask questions and don't forget to read more articles of mine about programming conceps. Peace ✌🏼
- Sebesta, R. W. (2019). Examples of Multiple Selectors. In Concepts of programming languages. NY, NY: Pearson.