MCS-011 Problem Solving and Programming

First year, Semester 1

Chapters

Newsletter

Types of Variables

In a program consisting of a number of functions a number of different types of variables can be found.

Global vs. Local Variables:

  • Global Variables: Recognized throughout the entire program.
  • Local Variables: Recognized only within the function where they are defined.


Static vs. Dynamic Variables:

  • Static Variables: Do not retain their value once the function execution is complete.
  • Dynamic Variables: Retain their value between function executions under certain conditions

 

Variables are characterized by their data type (type of value they represent) and storage class (permanence and scope within the program).

Storage Classes in C:

  1. Auto (automatic)
  2. Extern (al)
  3. Static
  4. Register


Automatic Variables

Automatic variables, also known as local variables, are declared within a function and their scope is limited to that function. This means that variables with the same name in different functions are treated as distinct and independent of each other. The automatic storage class is the default for variables declared within a function, so there is no need to explicitly use the auto keyword. All formal arguments passed to a function also have the automatic storage class. Automatic variables can be initialized in their declarations or through assignment expressions within the function. However, if an automatic variable is not initialized, it will hold an unpredictable value. Furthermore, the value of an automatic variable is not retained after the function exits.

For example, consider the following C code:

#include
void functionA() { int x = 10; // automatic variable printf("Value of x in functionA: %d\n", x); } void functionB() { int x = 20; // another automatic variable with the same name printf("Value of x in functionB: %d\n", x); } int main() { functionA(); functionB(); return 0; }

In this example, functionA and functionB both declare a variable x. Despite having the same name, these variables are distinct and only exist within their respective functions. The output will show the values of x as defined within each function, demonstrating the scope and uniqueness of automatic variables:

Value of x in functionA: 10
Value of x in functionB: 20

If an automatic variable like int y; is declared but not initialized, its value will be unpredictable until it is explicitly assigned. Additionally, once a function ends, any automatic variables declared within it lose their values, reinforcing their temporary and local nature.


External (Global) Variables

External, or global, variables are not confined to a single function; their scope extends from the point of declaration to the entire remaining program. This means they can be accessed and modified by any function within this scope, allowing values to be shared across multiple functions. The definition of an external variable is similar to any variable declaration and usually occurs outside or before the function that accesses it. This definition allocates the necessary storage space and can include initial values, which must be constants. If no initial value is provided, the variable is automatically assigned a value of zero. The extern specifier is not required when defining an external variable. However, if the external variable definition appears after the function definition, a declaration is necessary, beginning with the extern specifier and not allocating storage space. This distinction between definition and declaration ensures that external variables are correctly managed and accessible throughout the program.

For example, in the following C code:

#include
int globalVar = 10; // external variable definition and initialization void functionA() { printf("Value of globalVar in functionA: %d\n", globalVar); } void functionB() { globalVar = 20; // modifying the external variable printf("Value of globalVar in functionB: %d\n", globalVar); } int main() { functionA(); functionB(); functionA(); // checking the updated value in another function call return 0; }

globalVar is defined and initialized outside any function, making it an external variable. Both functionA and functionB can access and modify globalVar, demonstrating the shared nature of external variables across functions. This allows for consistent values to be used and updated throughout the program, enabling functions to communicate and operate on the same data. If globalVar were defined after the function definitions, each function would require an extern int globalVar; declaration to access it.


Static Variables

In single-file programs, static variables are defined within functions and, like automatic variables, have local scope to the function in which they are defined. However, static variables differ in that they retain their values throughout the execution of the program, maintaining their previous values between function calls. To declare a static variable, the static specifier precedes the declaration. These variables cannot be accessed outside their defining function, ensuring encapsulation within that function. Although static variables can have the same name as external variables, local static variables take precedence within their function, preserving the independence of external variables. Static variables must be initialized with constants, not expressions, and if no initial value is provided, they are automatically assigned a value of zero. Importantly, initialization occurs only once, during the first execution of the function.

For example, consider the following C code:

#include <stdio.h>
void functionA() { static int count = 0; // static variable initialization count++; printf("Count in functionA: %d\n", count); } void functionB() { static int count = 0; // another static variable with the same name count++; printf("Count in functionB: %d\n", count); } int main() { functionA(); functionA(); functionB(); functionB(); return 0; }

In this code, count is a static variable in both functionA and functionB. Despite having the same name, these variables are distinct within each function. The count variable in functionA maintains its value between calls to functionA, and the same is true for functionB. The output demonstrates how count increments with each call to the respective function, while retaining its value across multiple executions:

Count in functionA: 1 Count in functionA: 2 Count in functionB: 1 Count in functionB: 2

This example illustrates how static variables retain their state between function calls, allowing them to preserve information throughout the program's execution.


Register Variables

Register variables are a special type of storage class used to indicate that certain variables should be stored in the CPU's registers, which are fast, special storage areas used for arithmetic and logical operations. By storing variables in registers rather than memory, execution time can be reduced, resulting in smaller and more efficient programs with fewer instructions and data transfers. Register variables are declared using the register keyword, such as register int m;. However, if registers are unavailable, these variables are stored in memory and behave similarly to automatic variables.

Register variables have the same scope as automatic variables, meaning they are local to the function in which they are declared. Unlike other variables, register variables cannot be accessed using the address operator &, and pointers to register variables are not allowed. While they are typically used for integer types, other types with similar size, like short or unsigned, can also be declared as register variables. They are often employed in scenarios where efficiency is crucial, such as loop indices.

For example, in the following C code:

#include <stdio.h>
void processNumbers() { register int i; // register variable declaration for (i = 0; i < 5; i++) { printf("Processing number: %d\n", i); } } int main() { processNumbers(); return 0; }

The variable i is declared as a register variable, suggesting that it should be stored in a CPU register to speed up access. This is particularly useful in loops where the variable is frequently accessed. If registers are not available, i will be treated as an automatic variable and stored in memory, thereby functioning similarly to an automatic variable in that case. This approach can enhance the efficiency of the program by reducing the time spent on data transfers between memory and the CPU.

Report an issue

Reporting: Types of Variables (topic)

Related Posts