473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 2231
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: F. Da Costa | last post by:
Hi, Does anybody know why IE5+ does *not* honour array objects (like a table) across a session? Example: Frame A contains a var tableVar which is set via form Frame B (on init) using...
12
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the...
2
by: jmensch | last post by:
Hello. I'm a reasonably new ASP.NET programmer with no prior ASP or web development experience, but a lot of general programming experience. I'm using Visual Web Developer Beta Express 2005. ...
4
by: Rob | last post by:
Using VB.net and SQL server... I have a stored procedure that is simply returning a row count.... I know how to execute a stored procedure and add the output parameter. But how do I store...
1
by: suslikovich | last post by:
Hi all, I am getting this error when insert values from one table to another in the first table the values are varchar (10). In the second they are datetime. The format of the data is mm/dd/yyyy...
7
by: John Smith | last post by:
Hello, I have a simple question, I have a vb.net form with several buttons. If I store the name of a button in a variable.. Dim TheName as string TheName = ...
7
by: loial | last post by:
I need to store a list of variable names in a dictionary or list. I then later need to retrieve the names of the variables and get the values from the named variables. The named variables will...
4
karthickbabu
by: karthickbabu | last post by:
Is Possible to store a value to declared Variable from Text Box at run time. I want to store a value from Text Box in 2 dimension array. First Value can be sotred in variable(0,0). If i press enter...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.