Solution for Exercise 10-1

ex1001

#include <stdio.h>

void prompt(void);      /* function prototype */

int main()
{
    int loop;
    char input[32];

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

/* Display prompt */

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

Output

C:\DOS> hello?
C:\DOS> quit
C:\DOS> exit
C:\DOS> bye
C:\DOS> help!

Notes

* Two backslashes, \\, are used in the printf() function in Line 24. This expression is required so that a single backslash appears in the output, which is how the old MS-DOS prompt looked:

* It's possible to leave the parentheses empty for a void function, as in:

I prefer to use the word void. Otherwise, if the parenthesis are empty, it looks like I forgot something.

* The input buffer size (input[]) and the size argument of the fgets() function are identical; fgets() won't overflow the buffer when the values are equal.

* Your compiler may report an "ignored return value" warning for the fgets() statement. The function returns a pointer (covered in Chapter 18) or NULL when a string isn't read. That's a possibility, hence the warning message. At this point in your C education, it's okay to ignore this warning.

* By the way, the main() function doesn't lack arguments. These arguments need to be specified only when they're used. Regardless, do not put void in the main() function, as in main(void). This expression is incorrect and the compiler will barf.