Exercise 1-1

Run the “hello, world” program on your system. Experiment with leaving out parts of the program, to see what error messages you get.

Apart from briefly mentioning the cc command, the book does not go into much detail about the logistics of writing C programs, and perhaps that is for good reason. The second edition was published over forty years ago, and the C programming scene has changed quite a bit since then. Nowadays, by far the most widely used C compiler is gcc, the GNU C Compiler. macOS and most GNU/Linux systems come with gcc pre-installed. On Windows, the easiest way to start writing code is to install an IDE like CLion, which comes with everything you need to write C programs. The following steps assume you are using gcc; if you end up going the CLion route, you can check out its quick start guide.

To get started, open a text editor and write the following code.

    #include <stdio.h>
    
    main()
    {
        printf("hello, world\n");
    }

Save the file with a name ending in ".c." Technically, the file extension ".c" is only a convention (at least on *nix systems) but by default, gcc will refuse to compile files that do not end in ".c." To compile the code, run the command gcc /path/to/file.c. Within the file's directory, there should be another file called a.out, which is the resulting executable. You have written your first C program!

Note: in order to compile your code into a specific file, you can use gcc's -o option followed by the name you want to use for your output file (e.g. gcc /path/to/file.c -o execname.)

Let us see what happens when we make leave out parts of our code. If we get rid of the ;:

    #include <stdio.h>
    
    main()
    {
        printf("hello, world\n")
    }

gcc fails to compile the code and prints the following error message:

    <file>: In function 'main':
    <file>:5:29: error: expected ';' before '}' token

The compiler correctly detects that inside the function main, we are missing a semicolon before the closing brace. On the second line, after <file>, we see two numbers. It looks like they refer to the line and column where the error is.

Let us also delete the { to see how gcc reacts to multiple errors.

    #include <stdio.h>
    
    main()
    
        printf("hello, world\n")
    }

Now, gcc prints the following error messages:

    <file>: In function 'main':
    <file>:5:5: error: expected declaration specifiers before 'printf'
    <file>:7: error: expected '{' at end of input

The compiler no longer mentions the missing semicolon on line five. This is to be expected since the function itself is no longer valid, so the compiler does not know where its statements are located.

Let us try removing the #include statement at the top:

    int main()
    {
        printf("hello, world\n");
    }

This time, our program successfully compiles and runs as expected, but gcc warns us that we are missing the #include statement. This behavior should not be relied upon as other C implementations may not be able to compile this code.

    <file>: In function 'main':
    <file>:3:5: warning: implicit declaration of function 'printf'
    <file>:1:1: note: include '<stdio.h>' or provide a declaration of 'printf'

Try playing around with the file and see how the compiler reacts. Not everything it says might make sense yet, but hopefully, it will start to as we progress through the book.