[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

permanent variable

Posted on 1999-08-03
3
Medium Priority
?
253 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:roylam
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
Iexpert earned 200 total points
ID: 1264168
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
 
LVL 11

Expert Comment

by:alexo
ID: 1264169
Actually, sounds more like a static local variable:

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

    int previous = persistent;
    persistent = value;
    return previous;
}
0
 
LVL 11

Expert Comment

by:alexo
ID: 1264170
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

Featured Post

SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question