1. Show the lifetimes of the variables in the following C program and indicate which are statically vs. dynamically allocated.
int m;
void S () {
float z;
z = 1.0;
}
void R (int n) {
if (n > 0)
R(n-1);
S();
}
int main() {
m = 3;
R(1);
return 1;
}
Note that R will be activated twice, recursively.
2. In the following C program skeleton, identify all the name spaces by stating to which name space each name belongs. Also indicate the scope of each name. Note, C has static scoping.
int x, y, z;
int foo() {
{
int x;
}
}
foo2(int x) {
int y;
}
main() {
int y;
}
3.Can you explain what is wrong with the following Pascal program in terms of name-spaces and scopes? What about if Pascal had dynamic scoping?
program scope;
procedure foo()
begin
end;
procedure foo()
begin
end;
begin
foo();
end;
4. In terms of name spaces and scopes, is the difference between variable x and variable y in the following Pascal program?
program scope;
var x: integer;
procedure foo()
begin
end;
var y: integer;
begin
foo();
end;
5. Identify all the kinds of declarations in the following C program.
int x;
typedef int shortInt;
int foo(int z, int *q) {
int y;
return z + *q;
}
void main() { }
6. Using the following C program discuss what bindings exist in the environments (global environment, main environment, foo environment) at various points in the code. Note that C has static binding or scoping.
int x;
char hello;
int foo(int z, int q) {
int y;
return z + q;
}
void main() {
int y[30];
char *str;
str = "hello";
y[1] = foo(23, 34);
str = "good-bye";
}
7. Explain specifically why GOTO is considered harmful.
8. Does C have multiple assignment? Does it have collateral assignment?
9. Assume that the collateral operator is ``,'' and that the sequential operator is ``;''. Rewrite the following program using the collateral operators whenever you can (hint: look for read/write conflicts).
x = 4;
y = 23;
z = x + y;
printf("%d", z);
x = foo(x, y, z);
10. Discuss an important problem with the following code. Run the program and check the output of the program
#include<stdio.h>
int *foo() {
int x;
x = 2;
return &x;
}
int foo1(){
int y=675;
int z=90;
return y;
}
void main() {
int *x, *foo();
x = foo();
foo1();
printf("%d", *x);
}
11. Why is a language that only supports allocation of static storage items limiting?
12. In many programming languages strings are defined to be arrays of characters. Compare the consequences of this when a string variable is:
Consider string operations such as assignment, concatenation, and comparison.