469,133 Members | 1,179 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,133 developers. It's quick & easy.

How to store a variable value for more than one executions

Hi,
How to store a variable value for more than one executions.

Ex:

main()
{
int i=0;
i++;
printf("%d",i);
}

For first execution output should be 1
For Second execution output should be 2

nth exection output should be n

Any help is appreciated

Thnaks in Advance
Harish

Nov 14 '05 #1
12 1976
ha******@gmail.com wrote:
How to store a variable value for more than one executions.
More than one execution? Can't be done without resorting to external
storage, the only Standard kind of which are files.
main()
{
int i=0;
i++;
printf("%d",i);
}

For first execution output should be 1
For Second execution output should be 2


Like this, you can't do that. You'll need to write the value of i to a
file at the end of the program, and then read it back when you start the
next execution. There is no keyword like "static-but-larger", if that's
what you were looking for.

Richard
Nov 14 '05 #2
ha******@gmail.com wrote:
How to store a variable value for more than one executions.

Ex:

main()
{
int i=0;
i++;
printf("%d",i);
}

For first execution output should be 1
For Second execution output should be 2

nth exection output should be n


You could store the value of i in a file, or in an environment variable.
Nov 14 '05 #3
Grumble <de*****@kma.eu.org> wrote:
ha******@gmail.com wrote:
How to store a variable value for more than one executions.


You could store the value of i in a file, or in an environment variable.


In a file, yes, but how do you set an environment variable from C?

Richard
Nov 14 '05 #4
Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
Grumble <de*****@kma.eu.org> wrote:
ha******@gmail.com wrote:
How to store a variable value for more than one executions.


You could store the value of i in a file, or in an environment variable.


In a file, yes, but how do you set an environment variable from C?


On a Unix-like system, you can use the setenv(3) library function. I believe
this is also a documented part of the C90 standard, but I haven't checked it
to confirm.

| int
| setenv(const char *name, const char *value, int overwrite);

It's worth noting that an environment variable will persist across multiple
executions, provided they happen in the same environment. In other words, if
you log out, your environment is gone. If that's an issue, it's probably a
better idea to write a file instead.

- Philip

--
Philip Paeps Please don't email any replies
ph****@paeps.cx I follow the newsgroup.

A budget is trying to figure out how the family next
door is doing it.
Nov 14 '05 #5
Philip Paeps <ph***********@paeps.cx> wrote:
Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
Grumble <de*****@kma.eu.org> wrote:
ha******@gmail.com wrote:
> How to store a variable value for more than one executions.

You could store the value of i in a file, or in an environment variable.
In a file, yes, but how do you set an environment variable from C?


On a Unix-like system,


And in C? _ISO_ C, per the topic on this newsgroup?
I believe this is also a documented part of the C90 standard, but
I haven't checked it to confirm.


It is not.

Richard
Nov 14 '05 #6
ha******@gmail.com wrote:
Hi,
How to store a variable value for more than one executions.

Ex:

main()
{
int i=0;
i++;
printf("%d",i);
}

For first execution output should be 1
For Second execution output should be 2

nth exection output should be n

Any help is appreciated

Thnaks in Advance
Harish


Hey

If you have a static variable in a function and call that function from
main() then you would have access to the previous value of the
variable. For eg:

#include <stdio.h>

void func()
{
static int i=0;
i++;
printf("%d",i);
}

int main()
{
func();
func();
return 0;
}

So, when you 1st enter func(), i would be innitialised to 0. then its
incremented. Now when you again come to the same function, you would
have the last value of i (that is 1) and you can play with that value
and so on..

However, you want it for different executions of the function func() or
for different executions of the program ?

If its former then above solution should work fine, else if it's latter
than I guess copying the last value to a text file should work.

Thanks!! and have a nice day!!
Jaspreet

Nov 14 '05 #7
Philip Paeps <ph***********@paeps.cx> wrote:
On a Unix-like system, [snip] It's worth noting that an environment variable will persist across multiple
executions, provided they happen in the same environment. In other words, if
you log out, your environment is gone. If that's an issue, it's probably a
better idea to write a file instead.


[OT]
You're wrong. Environment is kept per process. The process finishes,
its environment is gone.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #8
Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
Philip Paeps <ph***********@paeps.cx> wrote:
Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
Grumble <de*****@kma.eu.org> wrote:
> You could store the value of i in a file, or in an environment
> variable.

In a file, yes, but how do you set an environment variable from C?


On a Unix-like system,


And in C? _ISO_ C, per the topic on this newsgroup?


You don't, it appears. You should be able to get an environment variable in
ISO C, but the way to set it is implementation defined. Thanks for pointing
that out. :-)
I believe this is also a documented part of the C90 standard, but I
haven't checked it to confirm.


It is not.


You're right. I should have checked. Only getenv() is documented:

| 7.20.4.5 The getenv function
| [...]
| The getenv function searches an environment list, provided by the host
| environment, for a string that matches the string pointed to by name.
| The set of environment names and the method for altering the environment
| list are implementation-defined.
| [...]

Sorry for straying off-topic there.

- Philip

--
Philip Paeps Please don't email any replies
ph****@paeps.cx I follow the newsgroup.

If builders built buildings the way programmers wrote
programs, then the first woodpecker that came along would
destroy civilisation.
Nov 14 '05 #9
On 03 Jun 2005 09:54:49 GMT, Philip Paeps
<ph***********@paeps.cx> wrote:
Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
Grumble <de*****@kma.eu.org> wrote:
> ha******@gmail.com wrote:
> > How to store a variable value for more than one executions.
>
> You could store the value of i in a file, or in an environment variable.
In a file, yes, but how do you set an environment variable from C?


On a Unix-like system, you can use the setenv(3) library function. I believe
this is also a documented part of the C90 standard, but I haven't checked it
to confirm.


No, it isn't. If you do man setenv you will probably see something like
"conforming to BSD 4.3" and possibly POSIX.1, not to the C standards.
However, it doesn't do what you want anyway, it modifies the current
environment not the parent environment so when the program exits that
environment is lost and set up from the environment of the parent
(shell) when the program is executed again.
| int
| setenv(const char *name, const char *value, int overwrite);

It's worth noting that an environment variable will persist across multiple
executions, provided they happen in the same environment.
No, it won't unless it is done in the parent environment. There is
nothing in Unix/POSIX which allows you to change the parent environment
(or in MSDOS, at least not portably from one version to another).
Followups set to comp.unix.programmer where it is on topic.
In other words, if
you log out, your environment is gone. If that's an issue, it's probably a
better idea to write a file instead.


That is the only portable way to do persistent variables in C (or most
other languages, although some provide library facilities to make it
easy).

Chris C
Nov 14 '05 #10
Hi,

If the executions are to be invoked automatically i.e., without manual
intervention ,
Then you can pass the value of ' i ' to the "main" by calling it
within the main itself.
If the execution has to be done manually then writing to file is a good
solution than disturbing environment variables.

The pseudo code would look like this.

main(arguments)
{

i=argc[1];
print(i++);
main(i);

}

If u want the execution to stop after certain point then embed the call
to main in an
if (condition)
{
main(i);
}

Sarath.B
IIIT-H.

Nov 14 '05 #11
"Sarath" <bs******@gmail.com> writes:
If the executions are to be invoked automatically i.e., without manual
intervention ,
Then you can pass the value of ' i ' to the "main" by calling it
within the main itself.
If the execution has to be done manually then writing to file is a good
solution than disturbing environment variables.

The pseudo code would look like this.

main(arguments)
{

i=argc[1];
print(i++);
main(i);

}

If u want the execution to stop after certain point then embed the call
to main in an
if (condition)
{
main(i);
}


main takes two arguments (an int and a char**) or none. You can't
legally call it with a single int.

In any case, a recursive call to main like this doesn't get you
anything that you can't do with a simple loop.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #12
Philip Paeps wrote:
On a Unix-like system,


Define Unix-like. And then show us how it relates to C standard.

-atl-
--
A multiverse is figments of its own creations
Nov 14 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Sanjay | last post: by
2 posts views Thread by jmensch | last post: by
4 posts views Thread by Rob | last post: by
7 posts views Thread by loial | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.