• C

permanent variable

i've a BIG program (open source) that is made up of many files what happen is that i want to have a variable that retain its value between different calls in that program.
ie. i just want to put a variable in a particular module which should retain its value from the previous call.

i'll charify more if this is not clear
roylamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IexpertCommented:
A non static global variable is what you need.
in the module you need to do the following:

-------------------------
/* module.c */
int global_variable=0;
-------------------------

Then in module.h do:

------------------------
/* module.h */
extern int global_variable; /* extern tells other modules that variable is elsewhere */
------------------------

Then in other modules do:

------------------------
/* other_module.c */
#include "module.h"

void Set_Global_Var(void)
{
    global_variable=1;
}
------------------------

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alexoCommented:
Actually, sounds more like a static local variable:

int SetValueAndReturnPrevious(int value)
{
    static int persistent = 0;

    int previous = persistent;
    persistent = value;
    return previous;
}
0
alexoCommented:
Or, a different approach with a static global variable:

Module.c:
--------------

static int persistent = 0;

void SetValue(int value)
{
    persistent = value;
}

int GetValue(void)
{
    return persistent;
}

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.