What does “static” means in a C program?

A “static” variable inside a function keeps its value between invocations.

For example:

#include <stdio.h>

void printValue();

int main()
{
  int i;
  for(i=0; i<5; i++)
  printValue();

  return 0;
}

void printValue()
{
  static int x = 0;
  int y =0;

  x++;
  y++;

  printf("x=%d y=%d\n", x, y);
}

 

Printout:

output

A “static” global variable or a function is “seen” only in the file it’s declared

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>