473,583 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2253
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.Be dBuG.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.progr ammer 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1911
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 top.A.tableVar = document.getElementById("someTable"); As long as Frame B is *not* 'refreshed/ reloaded' witk another page the
12
2362
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 graphics object of the form/pictureBox. Should It be better if make a graphics object from my pictureBox in load event handler of the form and store...
2
2121
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. I'm trying to find a way to store data in the URL of my pages. I know that SessionState does this if you go cookieless, and that's useful but it's...
4
2327
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 that value to a variable ?
1
5466
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 to be easily converted to dates. The conversion in this case is implicit as indicated in SQL Server documentation. Here is my query: INSERT INTO...
7
5710
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 = Me.btnMyLittleButton.Name.ToString How can I disable this button using the variable value?
7
2525
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 already have been created and given a value. I hope thats clear!!! How can I do this?
4
2762
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 text box will be cleared and get another number and it stored in variable(0,1) and so on I try like this, but not working any problem in this...
275
12169
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
7896
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7827
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7936
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8195
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5701
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1434
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.