Re: Need help in Global variable in c



<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


.



Relevant Pages

  • Re: Need help in Global variable in c
    ... you should avoid globals if possible. ... That depends entirely upon your platform (and possibly compiler ... The C language does not specify program behavior in ... You'll have a far better chance of getting accurate information about ...
    (comp.lang.c)
  • Re: Need help in Global variable in c
    ... how can I declare the global variable so that it can be set/get by ... you should avoid globals if possible. ... That depends entirely upon your platform (and possibly compiler ... The C language does not specify program behavior in ...
    (comp.lang.c)
  • RE: scope of public variables
    ... "Jim Thomlinson" wrote: ... If your globals are declared anywhere other than a standard code module ... is where did you declare your variable. ... Sub optCkforDupes_Click ...
    (microsoft.public.excel.programming)
  • Re: What is common practice?
    ... It is a question of style, Jim, but I don't like globals because: ... They lose their value when you reset your code, ... I also prefer to store these configuration settings/values as records instead of fields in this table. ... If you have a 1-record table which contains various settings for an application, and those settings are used frequently in different functions of the app, say via a function which opens the tbl_Settings recordset and returns the appropriate field, is it more common to run the function every time a setting is needed, or to use a global variable, and use the function only once at startup to assign the variable value. ...
    (microsoft.public.access.formscoding)
  • Re: Help with this code, please
    ... lineno +:= 1 ... args is a local variable within main, but you're trying to use it ... The order doesn't matter, globals may be declared ... prefer to declare all variables. ...
    (comp.lang.icon)