Variables & Global Variables 
Author Message
 Variables & Global Variables

Is "global variable" just another name for "variable" or are there
different types of variables?

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Wed, 10 Jul 2002 03:00:00 GMT  
 Variables & Global Variables

Quote:
> Is "global variable" just another name for "variable" or are there
> different types of variables?

Most people, when they say "global variable", mean file scope variables.
Some insist that only file scope variables with external linkage are
"global", whilst others seem to include file scope variables with
/internal/ linkage too. IIRC the C Standard doesn't use the term "global"
even once to describe C variables.

A file scope variable with internal linkage is defined outside any
function, and qualified by the keyword 'static'.

A file scope variable with external linkage is defined outside any
function, and /not/ qualified by the keyword 'static'. If it is qualified
with 'extern', it means the variable is not being defined, merely declared.

#include <stdio.h>

int foo; /* file scope variable with external linkage. Any function in any
module which declares it can see this variable (see below). This is the
definition. */

extern int bar; /* declaration of file scope variable with external linkage
defined elsewhere. */

static int baz; /* definition of file scope variable with internal linkage.
Only functions in this module can see this variable */

Wait a while for corrections by others here before taking all this as
gospel.

On the whole, many people feel file scope variables, at least of the
external linkage variety, are generally a bad idea, and are best avoided
where possible. But that's a design issue, not a language issue.

--
Richard Heathfield

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.

comp.lang.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Wed, 10 Jul 2002 03:00:00 GMT  
 Variables & Global Variables
On 22 Jan 2000 21:20:59 GMT, "Richard Heathfield"

Quote:

>A file scope variable with external linkage is defined outside any
>function, and /not/ qualified by the keyword 'static'. If it is qualified
>with 'extern', it means the variable is not being defined, merely declared.

This is usually true, but not always.

        extern int i = 0;

defines the variable i.



Wed, 10 Jul 2002 03:00:00 GMT  
 Variables & Global Variables


Quote:
> On 22 Jan 2000 21:20:59 GMT, "Richard Heathfield"

> >A file scope variable with external linkage is defined outside any
> >function, and /not/ qualified by the keyword 'static'. If it is
qualified
> >with 'extern', it means the variable is not being defined, merely
declared.

> This is usually true, but not always.

>    extern int i = 0;

> defines the variable i.

Indeed. I knew I'd miss something. :-(

Thanks for the correction.



Wed, 10 Jul 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Global variable or not global variable

2. static local variables vs global variables

3. global variables and external variables?

4. Global variables & scope issue

5. global variables & forms

6. Global variables & MFC

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

8. Help - I need to set variable A = value held in variable named in variable B

9. Why is not possible to assign a global variable to another global variable..?

10. Global variable

11. global variables

12. global variable

 

 
Powered by phpBB® Forum Software