CS157: Intro to C, Part II

Spring 2018

Static

See this page as a slide show

CS157 Static

The keyword static

The keyword static has several meanings in C:

  1. A static local variable endures—keeps its value.
  2. A static global variable is private to the file containing it.
  3. A static function is private to the file containing it.

The last two are really the same thing.

A local variable

int get_id_number() {
    int id=1000000;
    return ++id;
}

int main() {
    printf("First id:  %d\n", get_id_number());
    printf("Second id: %d\n", get_id_number());
    printf("Third id:  %d\n", get_id_number());
    return 0;
}
First id:  1000001
Second id: 1000001
Third id:  1000001

That doesn’t work very well.

Using a global

int id=1000000;
int get_id_number() {
    return ++id;
}

int main() {
    printf("First id:  %d\n", get_id_number());
    printf("Second id: %d\n", get_id_number());
    printf("Third id:  %d\n", get_id_number());
    return 0;
}
First id:  1000001
Second id: 1000002
Third id:  1000003

That’s ok, but global variables are evil.

A static local variable

int get_id_number() {
    static int id=1000000;
    return ++id;
}

int main() {
    printf("First id:  %d\n", get_id_number());
    printf("Second id: %d\n", get_id_number());
    printf("Third id:  %d\n", get_id_number());
    return 0;
}
First id:  1000001
Second id: 1000002
Third id:  1000003

It works, and has no global variables!

static global varaibles

int value;                  // global
void save(int n) {
    value = n;
}
int recall() {
    return value;
}

int main() {
    save(42);
    printf("That value was: %d\n", recall());
    return 0;
}
That value was: 42

It works if our whole program is in one file. If one file has a global int value and another has a global double value, then we’re in trouble.

static global varaibles

static int value;           // static global
void save(int n) {
    value = n;
}
int recall() {
    return value;
}

int main() {
    save(42);
    printf("That value was: %d\n", recall());
    return 0;
}
That value was: 42

value is now private to this file. It doesn’t interfere with anything in other files.

static functions

void foo() {
    puts("Hello from foo");
}
void bar() {
    foo();
}
int main() {
    bar();
    return 0;
}
Hello from foo

Consider a multi-file program. You might want bar to be visible to the other files, but not foo. Too bad—they’re both visible!

static functions

static void foo() {
    puts("Hello from foo");
}
void bar() {
    foo();
}
int main() {
    bar();
    return 0;
}
Hello from foo

Hooray! foo is now local to this file. It can’t be see from other files. Another file could have its own foo, and they won’t interfere.

static main

static int main() {
    puts("Hello, world!");
    return 0;
}
c.c:1: warning: 'main' is normally a non-static function
c.c:1: warning: 'main' defined but not used
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o: In function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status

Don’t do this. main has to be visible, so that it can be called by whoever calls main to kick things off.

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-04-25T20:44

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building