TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Wednesday, 6 May 2020

Static In C Language | Scope Of Static Variable In C Language | Static IN C Language In Hindi

Static In C Language

Static is a keyword used in C programming language. It can be used with both variables and functions, i.e., we can declare a static variable and static function as well.

 An ordinary variable is limited to the scope in which it is defined, while the scope of the static variable is throughout the program.

download the image of static in c language

Fig: Static In C Language 

Static keyword can be used in the following situations:

o    Static global variable


When a global variable is declared with a static keyword, then it is known as a static global variable. It is declared at the top of the program, and its visibility is throughout the program.

o    Static function


When a function is declared with a static keyword known as a static function. Its lifetime is throughout the program.

o    Static local variable


When a local variable is declared with a static keyword, then it is known as a static local variable. The memory of a static local variable is valid throughout the program, but the scope of visibility of a variable is the same as the automatic local variables.

o    Static member variables



When the member variables are declared with a static keyword in a class, then it is known as static member variables. They can be accessed by all the instances of a class, not with a specific instance.

o    Static method


The member function of a class declared with a static
keyword is known as a static method. It is accessible by all
the instances of a class, not with a specific instance.


Example:


   #include <stdio.h>  
    int main()   
   {  
    printf("%d",func());    
    printf("\n%d",func());  
     return 0;  
     }  
    int func()  
    {   
    int count=0; // variable initialization  
    count++; // incrementing counter variable  
       
    return count; } 

    output:


    1
    1

Static variable

A static variable is a variable that persists its value across the various function calls.

Syntax

    static data_type variable_name;

    Program of Static variable


    #include <stdio.h>  
    int main()  
    {  
   printf("%d",func());  
   printf("\n%d",func());  
     
    return 0;  
    }  
    int func()  
    {  
    static int count=0;  
    count++;  
    return count;  
     }  
   

     Output:


   1
   2

No comments:

Post a Comment