473,473 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

56 New Member
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 1227
weaknessforcats
9,208 Recognized Expert Moderator Expert
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 =...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.