473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How does a global variable declared as extern differ from a global variable declared

Say If I write

extern int y;

and then I write

int y ;

so I cannot access the y variable anywhere now since it is uninitialized while the second one is initialized to zero but we say that default value of extern varibale is zero so then why is this extern int y declaration used globally ?
Jul 26 '15 #1
1 1224
weaknessforcats
9,208 Expert Mod 8TB
Here you need to understand about linkage. There is internal linkage and there is external linkage.

External linkage allows you to use the variable in more than one file. Internal linkage allows you to use the variable in only the file where it is created.

By default the linkage of global variables is external.

So this code:

Expand|Select|Wrap|Line Numbers
  1. extern int i;
  2.  
  3.  
tells the compiler not to create a variable i because there is one already in another file that as external linkage.

Expand|Select|Wrap|Line Numbers
  1. extern int i = 10;
  2.  
  3.  
This tells the compiler to create a variable i with an initial value of 10. The extern here for external linkage is not necessary since linkage of global variables is external by default.

Expand|Select|Wrap|Line Numbers
  1. static int i;
  2.  
  3.  
This code says t create a variable i with an initial value of zero and with internal linkage. This variable is inaccessible outside the file where the variable is created. An extern i in another file cannot access this variable.

As to why global variables in the first place read this: http://bytes.com/topic/c/insights/73...obal-variables
Jul 26 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
1
by: seberino | last post by:
Python lets me access module level variables from *anywhere*. All I have to do is add module name in front. e.g. mymodule.myvariable Is this considered a 'global'? Or, does a 'global...
7
by: g | last post by:
hello! I wanna use a global variable,witch is the best way(I don't want to use a Singleton for an integer)? I try : #ifndef DUMY_H_ #define DUMY_H_ int connections; #endif /*DUMY_H_*/
18
by: StephQ | last post by:
I'm facing the following problem: I'm using the Gnu Scientific Library (it is a C math library), and in particular random number generation algorithms. Example code is: .... gsl_rng * r; .......
4
by: jubelbrus | last post by:
I need to initiate a global variable to a class before the initialization of any other global variable. The problem is that when I link the application with a dll, some or all global variables in...
8
by: yinglcs | last post by:
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...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
5
by: baburmm | last post by:
Hai, I want to get the Global static name(for Constant) Declared in the Global.asax file for my whole project as a constant. my coding in the Global.asax file is private static string strName =...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.