Re: Need help in Global variable in c
- From: Richard <rgrdev@xxxxxxxxx>
- Date: Sun, 30 Sep 2007 18:35:02 +0200
"Mike Wahler" <mkwahler@xxxxxxxxxxxx> writes:
<yinglcs@xxxxxxxxx> wrote in message
news:1191169232.898864.162910@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,
I read this article about global variable in c:
http://www.phim.unibe.ch/comp_doc/c_manual/C/SYNTAX/glo_int_vars.html
But I have a few questions
1. how can I declare the global variable so that it can be set/get by
other files?
/* glob.c */
int global = 99; /* defines 'global' */
/* glob.h */
extern int global; /* declares that 'global' */
/* exists elsewhere */
/* file1.c */
#include <stdio.h>
#include "glob.h"
int main()
{
global = 42;
printf("%d\n", global);
return 0;
}
In the interest of error prevention and maintainability,
you should avoid globals if possible.
2. If I have multiple instance of the same program running in the same
machine (linux), will each program instance has its own of global
variable?
That depends entirely upon your platform (and possibly compiler
settings). The C language does not specify program behavior in
a multi-process environment.
There is a platform where different instances share the same
"globals" as demonstrated above?
To the OP : Multiple instances all have their own data unless some
platform specific sharing mechanism is used. The code above is process
private in the real world.
I would be seriously interested to hear of cases where is not the case
as I have never seen it before.
.
-Mike
- Follow-Ups:
- Re: Need help in Global variable in c
- From: jacob navia
- Re: Need help in Global variable in c
- References:
- Need help in Global variable in c
- From: yinglcs@xxxxxxxxx
- Re: Need help in Global variable in c
- From: Mike Wahler
- Need help in Global variable in c
- Prev by Date: Re: Need help in Global variable in c
- Next by Date: Re: C program Questions
- Previous by thread: Re: Need help in Global variable in c
- Next by thread: Re: Need help in Global variable in c
- Index(es):
Relevant Pages
|