Solution for Exercise 10-2

ex1002

#include <stdio.h>

void prompt(void);      /* function prototypes */
void busy(void);

int main()
{
    busy();
    return(0);
}

/* Loop five times */

void busy(void)
{
    int loop;
    char input[32];

    loop=0;
    while(loop<5)
    {
        prompt();
        fgets(input,31,stdin);
        loop=loop+1;
    }
}

/* Display prompt */

void prompt(void)
{
    printf("C:\\DOS> ");
}

Notes

* Did you remember to prototype the busy() function? Yes, it was rude for me not to remind you in the Exercise, but it's a good way to learn.

* The order of the prototypes isn't crucial to the program's performance or preference. You can list prototypes in any order, though many programmers prefer to put them in the order they first appear in the code.

* The variables used in a function must be defined in that function. By copying Lines 7 through 16 into the busy() function, you also copied the declarations of the loop and input variables. Had you left them in the main() function, the busy() function wouldn't be able to access them. Variables in C are "local" to the functions in which they're defined.

* This code demonstrates how you can call one function within another function.