Interactive Bootstraps“Introduction” →Your first program

b41a5a2f2ebe0331704b93eb85adc4a46f9c2dff
🐶
Add the classic hello world program
This is a very simple program, which is also a good way to test that you
can get started with this book, by trying to compile this program.

🐱

Can you remind me how do I compile the program?

🐶

First, get up a Linux or macOS terminal.

If you are on Windows, I recommend Microsoft’s Windows Subsystem for Linux, which will get you a Linux system.

Once you have that, make sure you are in the directory containing hello.c. You can navigate inside a terminal using cd, find out where you are using pwd, and list the contents of the current directory using ls.

In this directory, run the command gcc hello.c -o hello. You might have to install gcc first, and if you are on macOS you could try using clang instead of gcc. Both gcc and clang are C compilers, which turn the hello.c text file into an executable binary file.

Finally, you can run the binary using ./hello, which should print Hello, world onto the terminal. If you have got to this point, well done, you are set for the rest of the book.

If you have not, then you might want to look into using a terminal a bit more, compiling C programs. If you are stuck, try navigating to the GitHub issues for this book, and you can ask a question there.

⟨hello.c⟩≡

@@ -0,0+1,6@@
1
+#include <stdio.h>

🐶

This includes functionality for input/output – stdio.h means standard library, input/output header.

🐱

What’s a header?

🐶

A header is just a text file containing details about functions, data-types and global variables. In this case it contains details about the printf function used below (as well as some other stuff).

2
+
3
+int main(int argc, char **argv) {

🐶

The main function is where the program starts. It’s inputs are argc which is an integer (int), and argv which is a pointer to a pointer to a character (char). The inputs to a function are called arguments. And the function eventually returns an integer (int).

4
+	// The classic C introduction program

🐶

In C, the characters // start a comment until the end of the line. Comments are for other programmers, and the computer ignores them.

🐱

But so are comments like this one, right?

🐶

Yes, but these comments are not part of the file, only the review/book. Whereas comments like the one above are part of the file. You can also create a comment using the characters /* which are until the characters */, not the end of the line.

5
+	printf("Hello, world\n");

🐶

Here we call the function printf to print Helo, world onto the screen. You use parentheses for the function call, and the double quotes (") to create a string.

🐱

What does \n mean?

🐶

It means a new line, as part of the string. The backslash \ starts an escape sequence, and the n means new line. Some other escape sequences are \" to put a double quote as part of the string, and \\ to put a single backslash as part of the string.

🐱

Why is it called printf and not just print?

🐶

It stands for print formatted – we will explore this in a little more detail next.

6
+}

🐱

Didn’t you say main returns an integer? Where is it?

🐶

The main function is special, if it doesn’t explicitly return an integer, it will implicitly return 0 indicating no error occurred. This is purely historical/convenience. But otherwise yes, all functions should return the values they promise to return.