Question
We know that C uses static scope. In this assignment, we imagine C uses dynamic scope and see what impact it would have. To help
We know that C uses static scope. In this assignment, we imagine C uses dynamic scope and see what impact it would have. To help you verify that you got the correct answers, I've given you the sum of aa+bb+cc for each function.
Remember that dynamic scope causes it to look for variables in the active call stack. You have to follow the flow of control to find them.
II. Pretend that C has dynamic scope instead of static scope. What would the output be?
14 entries, 2 points each.
cobra: aa= ____ bb=____ cc=____ aa+bb+cc= 350
rhino: aa=____ bb=____ cc=____ aa+bb+cc= 400
hippo: aa=____ bb=____ cc=____ aa+bb+cc= 295
hyena: aa=____ bb=____ cc=____ aa+bb+cc= 180
main: aa=____ bb=____ cc=____ aa+bb+cc=3030
// gcc -Wall scope.c -o scope
// ./scope
#include
int aa = 25;
int bb = 15;
int cc = 5;
static void cobra(int cc) {
aa += 20;
bb += 30;
cc += 40;
printf("cobra: aa=%4d bb=%4d cc=%4d aa+bb+cc=%4d ", aa, bb, cc, aa+bb+cc);
}
static void rhino(void) {
int cc = 100;
bb += 30;
cc += 10;
cobra(20);
printf("rhino: aa=%4d bb=%4d cc=%4d aa+bb+cc=%4d ", aa, bb, cc, aa+bb+cc);
}
static void hippo(int aa) {
aa += 40;
rhino();
printf("hippo: aa=%4d bb=%4d cc=%4d aa+bb+cc=%4d ", aa, bb, cc, aa+bb+cc);
}
static int hyena() {
int bb = 90;
hippo(80);
printf("hyena: aa=%4d bb=%4d cc=%4d aa+bb+cc=%4d ", aa, bb, cc, aa+bb+cc);
return 1000;
}
int main(int arc, char *argv[]) {
int bb = hyena();
cc += 2000;
printf("main: aa=%4d bb=%4d cc=%4d aa+bb+cc=%4d ", aa, bb, cc, aa+bb+cc);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started