{"id":5136,"date":"2024-11-21T21:52:58","date_gmt":"2024-11-21T20:52:58","guid":{"rendered":"https:\/\/x-engineer.org\/?p=5136"},"modified":"2024-11-21T21:53:51","modified_gmt":"2024-11-21T20:53:51","slug":"c-programming-static-variables","status":"publish","type":"post","link":"https:\/\/x-engineer.org\/c-programming-static-variables\/","title":{"rendered":"Static variables"},"content":{"rendered":"<p>This program demonstrates the use of <strong>static variables<\/strong> 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.<\/p>\n<pre>#include &lt;stdio.h&gt;  \/\/ For printf() function \r\n\r\n#define SIZE 5      \/\/ Size of arrays\r\n\r\n\/\/ Function declaration\r\nint* myAddition(int* x, int* y);\r\n\r\nint main(){\r\n\r\n    \/\/ Input arrays\r\n    int x[] = {1,2,3,4,5};\r\n    int y[] = {10,20,30,40,50};\r\n\r\n    \/\/ Output array\r\n    int* z = myAddition(x, y);\r\n\r\n    \/\/ Print table header\r\n    printf(\"x\\ty\\tz=x+y\\n\");\r\n\r\n    \/\/ Print array elements\r\n    for(int i=0; i&lt;SIZE; i++){\r\n        printf(\"%d\\t%d\\t%d\\n\", x[i], y[i], z[i]);\r\n    }\r\n\r\n    return 0;\r\n}\r\n\r\n\/\/ Function definition\r\nint* myAddition(int* x, int* y){\r\n\r\n    \/\/ Define static variable\r\n    static int z[SIZE];\r\n\r\n    for (int i=0; i&lt;SIZE; i++){\r\n        z[i] = x[i] + y[i];\r\n    }\r\n\r\n    \/\/ Return pointer to memory location\r\n    return z;\r\n}\r\n<\/pre>\n<h3>Code Explanation<\/h3>\n<p><strong>1. Header Files<\/strong><\/p>\n<ul>\n<li><code>#include &lt;stdio.h&gt;<\/code>: Provides the <code>printf()<\/code> function for output.<\/li>\n<\/ul>\n<p><strong>2. Macro Definition<\/strong><\/p>\n<ul>\n<li><code>#define SIZE 5<\/code> defines the size of the arrays for easy scalability and maintainability.<\/li>\n<\/ul>\n<p><strong>3. Main Function<\/strong><\/p>\n<ul>\n<li>two input arrays, <code>x<\/code> and <code>y<\/code>, are statically defined.<\/li>\n<li>a pointer <code>z<\/code> receives the address of the static array <code>z<\/code> in the <code>myAddition<\/code> function.<\/li>\n<li>a table is printed showing elements from the two input arrays (<code>x<\/code> and <code>y<\/code>) alongside their sums stored in <code>z<\/code>.<\/li>\n<\/ul>\n<p><strong>4. Static Variable in Function<\/strong><\/p>\n<ul>\n<li>the function <code>myAddition<\/code> defines a static array <code>static int z[SIZE]<\/code>.<\/li>\n<li>static variable behavior:\n<ul>\n<li>a static variable persists for the lifetime of the program but has local scope, meaning it retains its value across function calls.<\/li>\n<li>this allows the array <code>z<\/code> to remain accessible even after the function exits.<\/li>\n<\/ul>\n<\/li>\n<li>the function computes the element-wise sum of the input arrays and stores the result in <code>z<\/code>.<\/li>\n<li>the address of <code>z<\/code> is returned to the caller.<\/li>\n<\/ul>\n<p><strong>5. Output<\/strong><\/p>\n<p>The program prints a table with elements of <code>x<\/code>, <code>y<\/code>, and their summed values in <code>z<\/code>.<\/p>\n<pre>x       y       z=x+y\r\n1       10      11\r\n2       20      22\r\n3       30      33\r\n4       40      44\r\n5       50      55\r\n<\/pre>\n<p><strong>Conclusion<\/strong><\/p>\n<p>This code demonstrates the importance of <strong>static variables<\/strong> 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &lt;stdio.h&gt; \/\/ 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(&#8220;x\\ty\\tz=x+y\\n&#8221;); \/\/ Print array elements for(int i=0; <\/p>\n","protected":false},"author":2,"featured_media":5094,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[1642,1650,1643,300,1606,1651,1659,1660,1627,1641,1661,1658],"class_list":["post-5136","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c","tag-advanced-programming-techniques","tag-array-manipulation","tag-array-return","tag-c-programming","tag-code-optimization","tag-data-integrity","tag-data-persistence","tag-function-design","tag-function-scope","tag-memory-management","tag-static-memory-allocation","tag-static-variables","has_thumb"],"_links":{"self":[{"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/posts\/5136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/comments?post=5136"}],"version-history":[{"count":2,"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/posts\/5136\/revisions"}],"predecessor-version":[{"id":5138,"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/posts\/5136\/revisions\/5138"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/media\/5094"}],"wp:attachment":[{"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/media?parent=5136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/categories?post=5136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/x-engineer.org\/wp-json\/wp\/v2\/tags?post=5136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}