LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-21-2006, 11:38 AM   #1
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Rep: Reputation: 30
Variable available scope problem


hi guys, I am puzzled by the variable scope problem. I list my questions and answer by myself.
Any other cases and comments are welcome.

How to make a variable available to
1) Only One subroutine?
Aeclare&define it in subroutine.
eg. int Max(){
int len;
}

2) Several subroutines in one source file
3) all subroutines in one source file
A: Define it at the beginning of the source file and use it directly in subroutines.
eg.
int len;

int Max(){
len=1;
}
...
int Min(){
depth = len;

}
//changes of len in Max also changes the value in Min.

4) several subroutines in several source files
A. External declare it in head file. And define and ONLY define it(Is it correct?) in source files where the varialbe is firstly used.

eg. head.h

external int len;

source1.c
int len;

int Max(){
len = 99;
}

source2.c
int Min(){
depth = len(); // here depth is 99 too.

}
 
Old 05-22-2006, 01:16 AM   #2
pronoy
Member
 
Registered: Mar 2005
Location: india
Distribution: FC4, Debian Etch
Posts: 67

Rep: Reputation: Disabled
for no 3)
you should declare
static int len;

so that it will not be accessable from any other file.

for no 4)
I think there is no keywork 'external' in c, it should be 'extern'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how the scope of RHCE ? tofee Linux - Newbie 1 04-10-2006 07:44 AM
scope of an object and global scope lucky6969b Programming 7 12-09-2005 10:09 PM
java variable scope - use of "this" keyword zeppelin147 Programming 1 11-21-2005 11:04 PM
scope suchi_s Programming 1 10-30-2004 08:07 AM
question about scope in c++ PTBmilo Programming 7 02-10-2003 05:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration