What happens when we use static with a function it's self?

Thread Starter

microcontroller60

Joined Oct 1, 2019
41
If I use static variable inside the function, can it be of any use? I don't see any benefit. Even if I remove static keyword from the program, the output is the same

What happens when we use static with a function it's self?
Code:
#include<stdio.h>

int main() {

static int x = 5;
      x++;
      printf("x : %d", x);      
     
  return 0;
}
 

WBahn

Joined Mar 31, 2012
26,037
Consider the following:

C:
#include <stdio.h>
#include <stdlib.h>

int callee1(void)
{
    int i = 0;
    
    return ++i;
}

int callee2(void)
{
    static int i = 0;
    
    return ++i;
}

int main(void)
{
    int i;
    
    for (i = 1; i <= 5; i++)
    {
        printf("Iteration #%i\n", i);
        printf("   Callee1 has been called %i times\n", callee1());
        printf("   Callee2 has been called %i times\n", callee2());
    }
    
     return EXIT_SUCCESS;
}
 

Thread Starter

microcontroller60

Joined Oct 1, 2019
41
Consider the following:
That was not my question
C:
#include <stdio.h>
#include <stdlib.h>

int NoneStatic(void)
{
    int x = 0;
   
    return ++x;
}

int WithStatic(void)
{
    static int y = 0;
   
    return ++y;
}

int main(void)
{
    static int i;
   
    for (i = 1; i <= 5; i++)
    {
        printf("Iteration #%i\n", i);
        printf("   NoneStatic has been called %i times\n", NoneStatic());
        printf("   WithStatic has been called %i times\n",WithStatic());
    }
   
     return EXIT_SUCCESS;
}
In the above example, x will create and destroy at each function call. y is only initialized when first created and y will remember its value from the last time the function was called.

What happens when we use static variable inside in a main function ?
 

JohnInTX

Joined Jun 26, 2012
4,198
What happens when we use static variable inside in a main function ?
The same thing happens whether it is inside 'main' or any other function. The link I provided details what happens in the first hit:
Static local variables: variables declared as static inside a function are statically allocated while having the same scope as automatic local variables. Hence whatever values the function puts into its static local variables during one call will still be present when the function is called again.
@WBahn posted code that if you simply cut and pasted it into your computer and run it would quickly demonstrate what happens.

Not real sure what more you would need for this...
 

WBahn

Joined Mar 31, 2012
26,037
That was not my question

In the above example, x will create and destroy at each function call. y is only initialized when first created and y will remember its value from the last time the function was called.

What happens when we use static variable inside in a main function ?
Your question was, "What happens when we use static with a function it's self?"

You said nothing about restricting the function to being main() and only main().

Since main() is called only once, there is likely not going to be any discernible difference between using static and non-static variables since, from a practical standpoint, the primary difference is what happens the first time the function is called relative to what happens on subsequent calls. For main(), there are no subsequent calls -- unless YOU call main() recursively, which is generally not a good idea since you don't know what startup code the compiler put at the start of main().

In general, static variables are allocated on the heap while automatic variables are allocated on the stack (by most compilers, the language definition doesn't specify the mechanics of achieving the specified behavior). So most compilers will likely do that for static variables in main(), as well (but they might not). Even if they do and while that is a difference between the two situations, it's going to be hard to write code that is sensitive to that difference.
 

Thread Starter

microcontroller60

Joined Oct 1, 2019
41
In general, static variables are allocated on the heap while automatic variables are allocated on the stack (by most compilers, the language definition doesn't specify the mechanics of achieving the specified behavior). So most compilers will likely do that for static variables in main(), as well (but they might not). Even if they do and while that is a difference between the two situations, it's going to be hard to write code that is sensitive to that difference.
I have only one big doubt in which I always remain confused.

This line has given information about three things storage class, constant value and data type.
C:
static const short unsigned int x = 100;
I don't understand what to write in front of variable name when I define a variable in the code.
 

WBahn

Joined Mar 31, 2012
26,037
I have only one big doubt in which I always remain confused.

This line has given information about three things storage class, constant value and data type.
C:
static const short unsigned int x = 100;
I don't understand what to write in front of variable name when I define a variable in the code.
Then you need to learn what each of those qualifiers mean and then determine which ones to use depending one what you are trying to do.
 
Top