Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions