Question: Analyze the following code and answer the questions at the end: #include void useLocal(void); void useStaticLocal(void); void useGlobal(void); int x = 5; // global variable

Analyze the following code and answer the questions at the end:

#include

void useLocal(void);

void useStaticLocal(void);

void useGlobal(void);

int x = 5; // global variable

int main(void) {

int x = 7; // local variable to main

printf("local x in outer scope of main is %d ", x);

{ // start new scope

int x = 3; // local variable to new scope

printf("local x in inner scope of main is %d ", x);

} // end new scope

printf("local x in outer scope of main is %d ", x);

useLocal(); // useLocal has automatic local x

useStaticLocal(); // useStaticLocal has static local x

useGlobal(); // useGlobal uses global x

useLocal(); // useLocal reinitializes automatic local x

useStaticLocal(); // static local x retains its prior value

useGlobal(); // global x also retains its value

printf(" local x in main is %d ", x);

} // end main

// useLocal reinitializes local variable x during each call

void useLocal(void) {

int x = 9; // initialized each time useLocal is called

printf("local x in useLocal is %d after entering useLocal ", x);

++x;

printf("local x in useLocal is %d before exiting useLocal ", x);

} // end useLocal

// useStaticLocal initializes static local variable x only the first time

// the function is called; value of x is saved between calls to this function

void useStaticLocal(void) {

// initialized once

static int x = 31;

printf(" local static x is %d on entering useStaticLocal ", x);

++x;

printf("local static x is %d on exiting useStaticLocal ", x);

} // end useStaticLocal

// function useGlobal modifies global variable x during each call

void useGlobal(void) {

printf(" global x is %d on entering useGlobal ", x);

x *= 10;

printf("global x is %d on exiting useGlobal ", x);

} // end useGlobal

Answer the following questions:

What will be printed to the screen? HINT: Do the extra credit and youll see why

What is the difference between a variables scope and its storage duration?

What are the differences between a local variable and a global variable?

What are the differences between a static local variable and a nonstatic local variable?

Include a screenshot or copy the terminal output of the gdb interactive shell, where you set breakpoints at the underlined pieces of code and print the value of x at each breakpoint.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!