Static variables

This program demonstrates the use of static variables in C by defining a static array inside a function to store the results of adding two input arrays. The static array persists in memory after the function returns, allowing its data to be accessed in the calling function.

#include <stdio.h>  // For printf() function 

#define SIZE 5      // Size of arrays

// Function declaration
int* myAddition(int* x, int* y);

int main(){

    // Input arrays
    int x[] = {1,2,3,4,5};
    int y[] = {10,20,30,40,50};

    // Output array
    int* z = myAddition(x, y);

    // Print table header
    printf("x\ty\tz=x+y\n");

    // Print array elements
    for(int i=0; i<SIZE; i++){
        printf("%d\t%d\t%d\n", x[i], y[i], z[i]);
    }

    return 0;
}

// Function definition
int* myAddition(int* x, int* y){

    // Define static variable
    static int z[SIZE];

    for (int i=0; i<SIZE; i++){
        z[i] = x[i] + y[i];
    }

    // Return pointer to memory location
    return z;
}

Code Explanation

1. Header Files

  • #include <stdio.h>: Provides the printf() function for output.

2. Macro Definition

  • #define SIZE 5 defines the size of the arrays for easy scalability and maintainability.

3. Main Function

  • two input arrays, x and y, are statically defined.
  • a pointer z receives the address of the static array z in the myAddition function.
  • a table is printed showing elements from the two input arrays (x and y) alongside their sums stored in z.

4. Static Variable in Function

  • the function myAddition defines a static array static int z[SIZE].
  • static variable behavior:
    • a static variable persists for the lifetime of the program but has local scope, meaning it retains its value across function calls.
    • this allows the array z to remain accessible even after the function exits.
  • the function computes the element-wise sum of the input arrays and stores the result in z.
  • the address of z is returned to the caller.

5. Output

The program prints a table with elements of x, y, and their summed values in z.

x       y       z=x+y
1       10      11
2       20      22
3       30      33
4       40      44
5       50      55

Conclusion

This code demonstrates the importance of static variables in C when data needs to persist across function calls without being recreated. The use of static variables avoids dynamic memory allocation while ensuring that the data remains valid after the function exits. This is particularly useful for small, fixed-size data structures that are frequently reused. However, developers must use static variables carefully to avoid unintended side effects, as they are shared across function calls and can lead to data corruption in multi-threaded contexts. This technique is invaluable for efficient, low-overhead memory management in embedded systems or performance-critical applications.

Leave a Reply

Ad Blocker Detected

Dear user, Our website provides free and high quality content by displaying ads to our visitors. Please support us by disabling your Ad blocker for our site. Thank you!

Refresh