LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 03-04-2011, 04:04 AM   #1
dazzlinggopi
LQ Newbie
 
Registered: Jul 2010
Posts: 5

Rep: Reputation: 0
Exclamation What is the difference between scope,linkage and life of a variable in C language ?


As i went through some tutorials found no difference in their meaning .. please explain the difference in usage.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-04-2011, 04:19 AM   #2
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Scope and the life of a variable are the same things. The scope set determines a variable's currently set value.
See this example:
Code:
#include <stdio.h>

// Life/Scope limited to this whole program
int x = 30;

int main ()
{
     printf ("\nGlobal x: %d", x);

     // Life/Scope limited to this function
     int x = 10;
     
     {
	  // Life/Scope limited to these curly braces
	  int x = 20;
	  printf ("\nCurly x: %d", x);
     }     
     
     printf ("\nFunction x: %d", x);
     return 0;
}
 
Old 03-04-2011, 06:34 AM   #3
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by dazzlinggopi View Post
As i went through some tutorials found no difference in their meaning .. please explain the difference in usage.
There's only one ultimate "tutorial" to answer your questions: the C standard!! Please find one version of the C99 standard here. It has specific sections about scopes, linkages and lifetimes ...

Andi
 
1 members found this post helpful.
Old 03-04-2011, 06:50 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Anisha Kaul View Post
Scope and the life of a variable are the same things.
You are not correct.

Code:
int foo() { /* inside life of d, outside scope of d */
   static int x = 0;
   return ++x; }
int main() {
   int d;
   foo();
   d = 1;  /* inside life of x, outside scope of x */
   return foo(); }

Last edited by johnsfine; 03-04-2011 at 06:54 AM.
 
2 members found this post helpful.
Old 03-04-2011, 07:16 AM   #5
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by johnsfine View Post
You are not correct.
I forgot the static variable case!
 
Old 03-07-2011, 12:57 AM   #6
dazzlinggopi
LQ Newbie
 
Registered: Jul 2010
Posts: 5

Original Poster
Rep: Reputation: 0
It actually helped ..

Quote:
Originally Posted by ForzaItalia2006 View Post
There's only one ultimate "tutorial" to answer your questions: the C standard!! Please find one version of the C99 standard here. It has specific sections about scopes, linkages and lifetimes ...

Andi
Thanks for the help ... it helped me get the actual sense in using them ..
 
  


Reply



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
Variable scope in KSH v5.2.14 99/07/13.2 UltramaticOrange Programming 4 08-20-2008 10:41 AM
c++ - "index" is a global scope variable in <string>???? BrianK Programming 2 03-18-2008 08:05 PM
How to reference a variable outside it's block scope in C daYz Programming 8 10-03-2007 02:48 AM
Variable scope Ephracis Programming 5 07-28-2006 01:22 PM
Variable available scope problem ArthurHuang Programming 1 05-22-2006 12:16 AM

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

All times are GMT -5. The time now is 11:36 AM.

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