Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

permanent variable

Posted on 1999-08-03
3
Medium Priority
?
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

722 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