Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What type of data goes where?

+1 vote
asked Mar 30, 2022 by Shoukath Ali (130 points)
what type of data is stored in HEAP and STACK?

What are static and dynamic variables?

why do we need them?

1 Answer

0 votes
answered Apr 8, 2022 by Peter Minarik (88,520 points)

Heap vs Stack: read here.

In C, static variables are preceded by the static keyword. Their values are persisted beyond their visibility scope. They would keep their values between the execution of the same function.

Consider the following function. Run it and see that Foo() is called only once every time as the variable count is re-initialized and created every time Foo is called. This is a dynamic vairable.

If you uncomment the static keyword you'll see that the value of count is kept and not set back to 1 every time Foo() is called. This is a static variable.

#include <stdio.h>

void Foo()
    /*static*/ int count = 1;
    printf("Foo has been called %d times\n", count);

int main()
    return 0;

You would normally use dynamic variables. Their life span should be limited to their scope (the Foo() function in the above example, normally between an opening and closing curly brace). But you might want the variable to survive even beyond the scope, then you use static.

I hope this helps.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.