Jump to content

linkage, scope Question: someone enlighten me pls!!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
keithwb

keithwb

    Newbie

  • Members
  • Pip
  • 4 posts
tried to readup on the linkage, i noe there is internal linkage, external linkage, no linkage. but my understanding is still not enough to solve this question.

someone pls enlighten me, and hopefully with explanation.

thank in advance!


which of the four given statements is true about the following program?

int i = 0;

void f(long i);

void main (void){

	f(i);

}

void f (long i){

	long k = i;

	static int j = 1;

	if(j>1)

		k = i+2;

	j = j*2;

}

(a) variable i at line 1 has interenal linkage
(b) variable i at line 6 has function prototype scope
© variable j at line 8 has automatic storage
(d) varirable k at line 7 has no linkage

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
This screams "homework." We gladly help with homework, but we don't do it for you. Do you know what the difference between linkage and scope is?
sudo rm -rf /

#3
keithwb

keithwb

    Newbie

  • Members
  • Pip
  • 4 posts
i read up on linkage, in fact i see no much difference between linkage and scope.

for example, function prototype scope is same as internal linkage?

global scope is as external linkage?

i pretty confused.

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
No, but you're on the right track.

#include <bar.h>

void foo(int a, int b);
extern void bar(void);

int main(int argc, char **argv)
{
    foo(argc, argc+1);
    bar();
    return 0;
}

void foo(int a, int b)
{
    ... do something ...
}
Take a guess.

Edited by dargueta, 16 March 2010 - 12:01 AM.
Forgot code

sudo rm -rf /

#5
keithwb

keithwb

    Newbie

  • Members
  • Pip
  • 4 posts

dargueta said:

No, but you're on the right track.

#include <bar.h>

void foo(int a, int b);
extern void bar(void);

int main(int argc, char **argv)
{
    foo(argc, argc+1);
    bar();
    return 0;
}

void foo(int a, int b)
{
    ... do something ...
}
Take a guess.

try to print out the value of a and b in foo? sorry if that a noob answer, as i pretty new in this.

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
No, I meant try to guess the linkage of the functions, and the scope of the variables.
sudo rm -rf /

#7
keithwb

keithwb

    Newbie

  • Members
  • Pip
  • 4 posts

dargueta said:

No, but you're on the right track.


#include <bar.h>


void foo(int a, int b);

extern void bar(void);


int main(int argc, char **argv)

{

    foo(argc, argc+1);

    bar();

    return 0;

}


void foo(int a, int b)

{

    ... do something ...

}

Take a guess.

oh ok,

bar() is global scope, do not necessary have to put extern in front. it will still work.

foo() is global scope as well.

argc is external linkage?

int a and b is function protype scope.