C Program - Global, Private variable 
Author Message
 C Program - Global, Private variable

Hi,

can u please tell me how to define global variables when using multiple .c &
.h files ?

Thanx
--



Fri, 19 Aug 2005 16:53:34 GMT  
 C Program - Global, Private variable

Quote:
> can u please tell me how to define global variables when using multiple .c
&
> .h files ?
> can u please tell me how to define global variables when using multiple .c
&
> .h files ?

file1.c:
int myVariable = 0;

void func(some parameters)....
{
myVariable = 10;

Quote:
}

file2.c:
extern int myVariable;

void func2(void) {
myVariable = 1;

Quote:
}

--



Sat, 20 Aug 2005 02:19:16 GMT  
 C Program - Global, Private variable

Quote:

> Hi,

> can u please tell me how to define global variables when using multiple .c &
> .h files ?

int global;
main(){}

hthy
micha

--
My family history begins with me, but yours ends with you.
                 -- Iphicrates
--



Sat, 20 Aug 2005 02:19:18 GMT  
 C Program - Global, Private variable

Quote:
> can u please tell me how to define global variables when using multiple .c
&
> .h files ?

file1.c:
int myVariable = 0;

void func(some parameters)....
{
myVariable = 10;

Quote:
}

file2.c:
extern int myVariable;   /* may be placed in a .h */

void func2(void) {
myVariable = 1;
...etc...

Quote:
}

--



Sat, 20 Aug 2005 02:19:25 GMT  
 C Program - Global, Private variable

Quote:

> can u please tell me how to define global variables when using multiple .c &
> .h files ?

Rule 1:  Never *define* an object or function in a header.
Rule 2:  *Declare* every external (linkable) object and
function in at least one header.
Rule 3:  Whenever a reference is made to an object or
function, be sure that *that* source file includes the
header containing the corresponding declaration.
Rule 4:  *Define* an object or function in exactly *one*
..c file.  (Specifying an initializer for a declaration
defines an object.)

There are a few ways to cheat, but don't worry about them
until you've mastered the above approach.
--



Sat, 20 Aug 2005 02:19:27 GMT  
 C Program - Global, Private variable
On 03 Mar 2003 08:53:34 GMT, "Phoenix"

Quote:

>Hi,

>can u please tell me how to define global variables when using multiple .c &
>.h files ?

--- globalvar.h ---
extern int var1;
extern int var2;

--- file1.c ---
#include "globalvar.h"
int var1;
void dummy1(void)
{
   var1 = var2;

Quote:
}

--- file2.c ---
#include "globalvar.h"
int var2;
void dummy2(void)
{
   var2 = var1;

Quote:
}

Regards,

                               -=Dave
--
Change is inevitable, progress is not.
--



Sat, 20 Aug 2005 02:19:29 GMT  
 C Program - Global, Private variable
I usually create one global.c where all the globals are defined,
and a global.h with the "extern" versions which can be included by
all the .c files.

Alternatively, put each definition in the .c file where it is
mostly used, and put the extern in its associated .h file.

Quote:

> can u please tell me how to define global variables when using multiple .c &
> .h files ?

--
--

--



Sat, 20 Aug 2005 02:19:31 GMT  
 C Program - Global, Private variable

Quote:

> can u please tell me how to define global variables when using
> multiple .c & .h files ?

u who? u thant is no longer sec. gen of the U.N.

Assuming you meant 'you', one method is to define the variable
(without the static qualifier) in the appropriate .c file and
declare it with an extern qualifier in the .h file for that .c
file.  Then #include that .h file in any other .c file that
requires a reference to the variable.

As in English, spelling counts.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!
--



Sat, 20 Aug 2005 02:19:42 GMT  
 C Program - Global, Private variable

in comp.lang.c.moderated:

Quote:

> > can u please tell me how to define global variables when using multiple .c &
> > .h files ?

> Rule 1:  Never *define* an object or function in a header.
> Rule 2:  *Declare* every external (linkable) object and
> function in at least one header.
> Rule 3:  Whenever a reference is made to an object or
> function, be sure that *that* source file includes the
> header containing the corresponding declaration.
> Rule 4:  *Define* an object or function in exactly *one*
> ..c file.  (Specifying an initializer for a declaration
> defines an object.)

Addendum to Rule 4:  Declaring an object at file scope *without* the
extern keyword also *defines* the object and provides a default
initialization of 0 or NULL, respectively, for arithmetic and pointer
types.

Rule 5: *Include* the header that *declares* the object or function
with external linkage in the *.c* file that *defines* the object or
function in question.

Rule 5 is one of the most important.  Otherwise changes between the
declaration in the header and the definition in the source file can,
and often do, get out of sync.  Some linkers catch some of these sync
errors, some don't catch any, no linker that I know of catches them
all.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
--



Sat, 20 Aug 2005 13:23:56 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Global variable or not global variable

2. Uninitialized global data (was Re: Global variables)

3. Private, public, and global objects?

4. global static vs private

5. static local variables vs global variables

6. Variables & Global Variables

7. global variables and external variables?

8. CONFLICT - Global Var of a .LIB Vs Global Var of a program which uses the LIB

9. Newbie: separate big .cs file into small .cs files

10. Should methods in class use its own properties or private variables

11. forms designer screws up private variables

12. Assigning initial values to private variables in a class

 

 
Powered by phpBB® Forum Software