How to store a variable value for more than one executions

H

harishg2

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
 
R

Richard Bos

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
 
G

Grumble

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.
 
R

Richard Bos

Grumble said:
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
 
P

Philip Paeps

Richard Bos said:
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
(e-mail address removed) I follow the newsgroup.

A budget is trying to figure out how the family next
door is doing it.
 
J

Jaspreet

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
 
S

S.Tobias

Philip Paeps said:
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.
 
P

Philip Paeps

Richard Bos said:
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. :)
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
(e-mail address removed) I follow the newsgroup.

If builders built buildings the way programmers wrote
programs, then the first woodpecker that came along would
destroy civilisation.
 
C

Chris Croughton

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
 
S

Sarath

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.
 
K

Keith Thompson

Sarath said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top