Show the symbol table for the following C programs at the printf lines (a) using
ID: 3760919 • Letter: S
Question
Show the symbol table for the following C programs at the printf lines
(a) using lexical scope and (b) using 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
Answer:
Program 1:
(a)
Using lexical scope, the scope of the each variable declared with in the function exists only with in the function. Its image is not available to the outside of the function.
So, according to program 1:
Variable “b” is declared as constant. A constant variable once created cannot be changed.
In “foo()” function, the b is not declared with in the function. Hence, “b” takes the constant value 10. It is added with value 10 and stored in the declared variable “a” and it returns value of “a”.
So, it prints 20.
When “bar()” function is called from the main function, it contains, a variable “b” declare inside the function. So, its scope exists within the function “bar()”. Again inside the function it calls the “foo()” function, so variable “b” loses its scope and takes the value 10 and returns value of “a” as 20 and is printed.
(b)
In Dynamic programming, the value of global variable exists everywhere until and unless a local variable is declared and initialized.
So, according the program 1:
When “foo()” function is called from the main function, it prints the value of b as 10, add the value to 10 and returns the value of a=20 and is printed.
Similarly, when “bar()” function is called from the main function, then in the local variable b is initialized with b = 4. But, when the “foo()” function is called, it loses the scope of b value and prints the value of “a” as 20.
Here in both the cases, same functionality is done.
Program 2:
(a)
Using lexical scope:
In the “first()” function, the variable a is not declared. So, the value of “a” is not defined.
In the “second()” function, the variable a is declared and is initialized with value 6. It internally calls the “first()” function where a is not declared again. Hence, a is just initialized with value 6. In the print method of main function it only prints the value of “a” as 6.
(b)
Using Dynamic programming:
In Dynamic programing, it takes the global variable into consideration. Hence, even though value of “a” in the main function is initialized as 4, the second() function is called, so its value is changed to 6, then first() function is called, where “a” is initialized as 12. So, globally, the value of “a” is modified to 12.
Therefore, the value of “a” is printed as 12.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.