Introduction to C: Booleans, While Loops, and Comparing Strings

Now that we've got a player structure and a basic input function, let's build on that to create an input loop, and write some code to recognize a few commands.

Modifying main.c

For now, let's add the code for the input loop into main.c. First we need a variable to indicate if the user is still playing our game or not. We'll use a boolean for this.

Booleans in C

A boolean (or bool, in C) is a value that can be true or false.

Older versions of the C standard didn't include a bool type. Starting with the C99 standard, C supports a bool type when stdbool.h is included.

Generally, C considers any integer with a non-zero value to be true, and any integer with a zero value to be false. Consequently, using the bool type is optional. Therefore, you may find that older programs written in C will use preprocessor macros to define their own boolean types, such as by writing #define TRUE 1 and #define FALSE 0.

We'll define our boolean at the top of the main() function as follows:

    bool playing = true;

The while() Loop

A while loop repeatedly executes the statements following it (inside the the curly braces - { and }) until the condition defined in it becomes false.

We'll define our while loop between the calls to player_print() and player_free() as follows:

    while(playing) {
        printf("> ");
        line = input_getline();
    }

This is an infinite loop. It will cause our program to continue requesting input until it is terminated. We'll need a way to set playing to false in order to exit our while loop.

Strings in C

Unlike higher-level languages, C does not provide a built-in way to compare strings. (You may have noticed that we haven't used a string type.) Rather than a built-in type, C programs tend to treat characters as sequences of characters, terminated by a zero. This might be better visualized on a per-character basis; for example, consider the string "Hello". It can be represented with six bytes (a char in C is typically one byte) as follows:

Index 0 1 2 3 4 5
char 'H' 'e' 'l' 'l' 'o' '\0'
int 72 101 108 108 111 0
Using strcasecmp()

POSIX defines a set of useful string comparison functions. (You can look at the manual for those functions by typing man string.)

For this exercise, we'll compare the string the user entered to commands we would like to recognize. For example, if the user types quit, we will exit the program. Since we don't care if the user enters QUIT, quit, or Quit (or any other variation), we'll use the strcasecmp() function to compare the strings without regard to case.

To stop the infinite loop, we'll add the following code to the inside of our while() loop, after getting the line of input:

        if(strcasecmp("quit", line) == 0) {
            playing = false;
        }

The strcasecmp() function compares the two strings (again, without regard to case), and returns a value of 0 if the strings are equal.

With that, if the user types quit, the loop will stop (and thus, the program will terminate).

Writing a Few More Commands

Following this pattern, we'll add a few more commands to the loop. (Take a look at the code on GitHub for the full source code.)

        if(strcasecmp("quit", line) == 0) {
            playing = false;
        } else if(strcasecmp("status", line) == 0) {
            player_print(player);
        } else if(strcasecmp("help", line) == 0) {
            printf("Available commands:\n");
            printf("    status - Show player status.\n");
            printf("    help   - Show this help screen.\n");
            printf("    quit   - Exit the program.\n");
        } else {
            printf("Unknown command. Type \"help\" for help, \"quit\" to exit the program.\n");
        }

With that, users can quit the program, see the status of their player, and get help on the available commands. If the user enters a string that doesn't match a command, an Unknown command message will be printed.

Conclusion

By now, you learned how to define a bool in C, how to create a basic while loop, and how to use strcasecmp().

In the next lesson, we'll take a look at using the valgrind tool to troubleshoot memory leaks in our program.