Static Keyword ( C )

  • Static Global variables: Variable is initialised only once and then it is reused. Variables can be used anywhere in the same file only. Scope is said to be the complete file, Lifetime is the complete program.

#include<stdio.h>

static int a;

int main(){

  printf("%d \n",a); // will print 0 


  return (0);
}        

  • Static variables in Functions (including main function): Variable is initialised only once and then it is reused. Scope is said to be the Function in which it is initialised, Lifetime is the complete program.

#include<stdio.h>

void function(){
  static int b=0;
  b++;
  printf("%d, ",b); // will print 1, 2, 3, 

  c++; //ERROR: 'c' was not declared in this scope
  printf("%d, ",c); //ERROR: 'c' was not declared in this scope

}

int main(){

  for(int i=0 ; i<3 ; i++){
    function();
  }

  static int c=2;
  c++; 
  printf("%d, ",c); //will print 3, 

  return (0);

}        

  • Static Functions : Functions cannot be called from other files of the project.

// main.c

static func(){
  return (0) ;
}

int func_1(){
  return (10) ;
}

int main (){
  printf("%d \n", func() ); //will print 0 
  printf("%d \n", func_1() ); //will print 10
  printf("%d \n", func_2() ); // ERROR: Func_2() not declared in this scope. 
  printf("%d \n", func_3() ); // will print 20

  return (0);
}

        

let's say, another file is also a part of the same project.

//file_1.c

static int func_2(){
  return (10+func()); //ERROR: func() not declared in this scope.
}

int func_3(){
return (20);
}        


Miscellaneous implications:

  • Static variables are stored in data segment.
  • Static variables cannot be declared inside structure.

要查看或添加评论,请登录

Yash Joshi的更多文章

  • Big Endian vs little Endian

    Big Endian vs little Endian

    Big Endian: Starts with Big Byte Little Endian: Starts with small/little Byte e.g: Lets have a number to store in…

  • Encapsulation vs Abstraction

    Encapsulation vs Abstraction

    TL;DR My humble attempt at understanding and documenting the famous and confusing OOPS concepts. pure Encapsulation:…

  • LeetCode: 119. Pascal's Triangle II

    LeetCode: 119. Pascal's Triangle II

    Question: Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle. In Pascal's…

  • I2C Protocol

    I2C Protocol

    IIC: Inter Integrated Circuit -Inter ICs: External to ICs. -Intra ICs: Internal to ICs.

  • Simple Linux Device Driver

    Simple Linux Device Driver

    I wrote a simple device driver, but wanted to get my hands a bit more dirty. Went ahead and wrote the same device…

    1 条评论
  • Compilation Stages in C Program.

    Compilation Stages in C Program.

    1) Pre-Processing 2) Compiling 3) Assembling 4) Linking Lets take a look at each one Briefly: 1) Pre-Processing:- Key…

    3 条评论
  • Lifetime vs Scope of a variable ( C )

    Lifetime vs Scope of a variable ( C )

    Lifetime: The time for which the variable holds a valid memory address with a valid value irrespective of, if someone…

社区洞察

其他会员也浏览了