Skip to main content

Command Palette

Search for a command to run...

static

Published
•1 min read•View as Markdown

Program 1:

#include <stdio.h>
int a, b =0;
void prtFun (void);

int main (){
    static int a = 1; /* line 1 */

    prtFun();

    a++;

    prtFun();

    printf ("Inside main, a is %d and b is %d\n" , a, b);
}

void prtFun (void){
    static int a = 1; /* line 2 */
    int b = 10;
    a+=b;
    b++;
    printf ("Inside function, a is %d and b is %d\n" , a, b);

}

Output:

Program 2:

#include <stdio.h>
static int sum(int num1, int num2) {
    return num1 + num2;
}
int main() {
    int a = 5, b = 3;
    int result = sum(a, b);
    printf("Sum: %d\n", result);
    return 0;
}

Fact: Access to static functions is restricted to the file where they are declared. Apart from that there is no difference between a normal and a static function in C.

35 views

More from this blog

P

Programming Tutorials

89 posts