Show the symbol table for the following C programs at the print lines using (a)
ID: 3761214 • Letter: S
Question
Show the symbol table for the following C programs at the print lines using (a) the lexical scope and (b) the dynamic scope.
What does the program print using each kind of scope rule?
Program 1:
const int b = 10;
int foo()
{
int a = b + 10;
return a;
}
int bar()
{
int b = 4;
return foo();
}
int main()
{
printf(“foo = %d ”,foo());
printf(“bar = %d ”,bar());
return 0;
}
Program 2:
int a;
void first()
{
a = 12
}
void second()
{
int a = 6;
first();
}
void main()
{
a = 4;
second();
printf("%d ",a);
}
Explanation / Answer
Program 1:
const int b = 10;
int foo()
{
int a = b + 10;
return a;
}
int bar()
{
int b = 4;
return foo();
}
int main()
{
printf(“foo = %d ”,foo()); //prints 20//
printf(“bar = %d ”,bar()); //prints 20//
return 0;
}
Static(lexical) scoping : Prints 20
prints 20
Dynamic scoping : prints 20
prints 14
Symbol Name
Type
Scope
foo
function,int
global
bar
function,int
global
a
int
block local
b
int,const
Global,block local
Program 2:
int a;
void first()
{
a = 12;
}
void second()
{
int a = 6;
first();
}
void main()
{
a = 4;
second();
printf("%d ",a);
}
Static(lexical) scoping : Prints a:12
Dynamic scoping : prints a:12
Symbol Name
Type
Scope
first
function
global
second
function
global
a
int
block local,global
Symbol Name
Type
Scope
foo
function,int
global
bar
function,int
global
a
int
block local
b
int,const
Global,block local
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.