c99/c89

S

shaanxxx

Keith said:
Please don't snip attribution lines, and provide enough context so
your followup makes sense on its own. I happen to know what you're
talking about because I've read the previous articles in the thread;
if I hadn't, your followup wouldn't have made any sense at all.


sorry for that. My follow up was not related to topic , so I removed
that.
 
C

Chris Dollin

Frederick said:
Keith Thompson posted:


They are equivalent, are they not?

Even in C, this is not true, as static variables indicate.
As per my current understanding, an
object's lifetime ends when it goes out of scope.

The notions of "scope" and "lifetime" are not specific to C, and in
general, no, an object's lifetime need not end when it goes out
of scope: Scheme, Common Lisp, Pop11, Smalltalk, and a whole raft
of pure-functional languages exploit this.
 
J

Joe Wright

pete said:
Scope is related to linkage.
Lifetime has nothing to do with linkage.

I think of linkage as how the linker can rationalize two or more
translation units. I see scope as having to do with visibility rather
than linkage. Imagine..

int i = 42;

int foo(int x) {
int j = x + i;
return j;
}

int main(void) {
int j = foo(10) + i;
return 0;
}

The scope of i makes it visible from both main and foo. main:j goes out
of scope when foo is called. foo:j is the only j seen in foo. No
conflict. When foo returns, foo:j is out-of-scope and only main:j is
visible.
 
P

pete

Joe said:
pete wrote:

I think of linkage as how the linker can rationalize two or more
translation units. I see scope as having to do with visibility rather
than linkage.

What do you think linkage is?

N869
6.2.2 Linkages of identifiers
[#1] An identifier declared in different scopes or in the
same scope more than once can be made to refer to the same
object or function by a process called linkage. There are
three kinds of linkage: external, internal, and none.
 
S

shaanxxx

pete said:
Joe said:
pete wrote:

I think of linkage as how the linker can rationalize two or more
translation units. I see scope as having to do with visibility rather
than linkage.

What do you think linkage is?

N869
6.2.2 Linkages of identifiers
[#1] An identifier declared in different scopes or in the
same scope more than once can be made to refer to the same
object or function by a process called linkage. There are
three kinds of linkage: external, internal, and none.

I have file a f1.c which contain

int i =0 ;

and i have a file f2.c which contains

extern int i;
int fx()
{ return i}


If i combine this two file together (compile and link). things will
work fine .


now i change file f1.c to

static int i =0;

file f2 will compile. and i will get linkage error.

As per my understand . identifier `i` was in scope of file f2.c so f2.c
got compiled.
since object has internal linkage, i got error at the time of linking.

correct me if i am wrong , " linkage defines where object is available.
scope defines where 'identifier name' is available."

Definately, we cant link to object without using identifier name,
like if i remove 'extern int i' statement from f2.c.


any comments ?
 
J

Joe Wright

shaanxxx said:
pete said:
Joe said:
pete wrote:
Scope is related to linkage.
Lifetime has nothing to do with linkage.
I think of linkage as how the linker can rationalize two or more
translation units. I see scope as having to do with visibility rather
than linkage.
What do you think linkage is?

N869
6.2.2 Linkages of identifiers
[#1] An identifier declared in different scopes or in the
same scope more than once can be made to refer to the same
object or function by a process called linkage. There are
three kinds of linkage: external, internal, and none.

I have file a f1.c which contain

int i =0 ;

and i have a file f2.c which contains

extern int i;
int fx()
{ return i}


If i combine this two file together (compile and link). things will
work fine .


now i change file f1.c to

static int i =0;

file f2 will compile. and i will get linkage error.
Because you have removed external linkage of int i if f1.c, the
declaration extern int i in f2.c cannot be resolved.
As per my understand . identifier `i` was in scope of file f2.c so f2.c
got compiled.
Yes.

since object has internal linkage, i got error at the time of linking.
Yes, internal linkage in f1.c and extern in f2.c and the linker cannot
resolve int i.
correct me if i am wrong , " linkage defines where object is available.
scope defines where 'identifier name' is available."
Actually they both have to do with names. Linkage has to do with using
functions and variables in other modules or libraries. During the course
of compiling and assembling a particular translation unit, the assembler
figures out which functions and variables are referred to but not
defined in the current translation unit. These unresolved references are
recorded in a symbol table, part of the object file.

Any number of translation units are treated this way and produce object
files with symbol tables.

The last step of building our program is linking. The linker combines
the object files and any library files resolving references as it goes.
Definately, we cant link to object without using identifier name,
like if i remove 'extern int i' statement from f2.c.


any comments ?

But scope is different. It has to do with whether a particular
identifier can be seen from where you are.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top