Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,861 questions

51,782 answers

573 users

How to define and use static variable in a function in C

1 Answer

0 votes
#include <stdio.h>

void functionWithStatic(void);
void function(void);

int main(int argc, char **argv) 
{ 
    functionWithStatic();
    function();
    functionWithStatic();
    function();
    functionWithStatic();
    function();
    
    return(0);
}

void functionWithStatic(void)
{
   static int n = 0;
    
   n++;
   
   printf("static int n = %d\n", n);
}

void function(void)
{
   int x = 0;
    
   x++;
   
   printf("int x = %d\n", x);
}

/*
run:

static int n = 1
int x = 1
static int n = 2
int x = 1
static int n = 3
int x = 1

*/

 



answered Jun 12, 2015 by avibootz

Related questions

2 answers 152 views
1 answer 272 views
1 answer 201 views
1 answer 135 views
135 views asked Dec 27, 2020 by avibootz
1 answer 88 views
1 answer 237 views
1 answer 192 views
192 views asked Jun 12, 2015 by avibootz
...