Re: Need help in Global variable in c
- From: "Mike Wahler" <mkwahler@xxxxxxxxxxxx>
- Date: Sun, 30 Sep 2007 09:28:09 -0700
<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.
-Mike
.
- Follow-Ups:
- Re: Need help in Global variable in c
- From: Richard
- Re: Need help in Global variable in c
- References:
- Need help in Global variable in c
- From: yinglcs@xxxxxxxxx
- Need help in Global variable in c
- Prev by Date: Re: Macro for supplying memset with an unsigned char
- Next by Date: Re: Need help in Global variable in c
- Previous by thread: Need help in Global variable in c
- Next by thread: Re: Need help in Global variable in c
- Index(es):
Relevant Pages
|