Skip to content

Handout: Hello, World in C

Your first C program. Follow along in lecture, then use this to review on your own. The goal is to get comfortable with the edit -> compile -> run loop.


Terminal

We'll be working in the terminal, so let's start there. Open a terminal window in your operating system:

  • macOS: Search for "Terminal" in Spotlight (Cmd + Space).
  • Linux: Search for "Terminal" in your applications menu.
  • Windows: Use "Command Prompt" or "PowerShell" (search in Start menu), or install Windows Subsystem for Linux (WSL) for a more Unix-like experience.

For this first in-class exercise, we will use SSH to connect to a remote server to write your first C program. By the end of the week, we will transition to using your local machine for development.

Connecting to the server

To connect to the server, open your terminal and run the following command:

ssh <your-cnet-id>@linux.cs.uchicago.edu

Replace <your-cnet-id> with your actual CNet ID. You will be prompted to enter your password. Once you are logged in, you will see a welcome message and a command prompt.

Create a directory for this lecture

Before we start writing code, let's create a directory for this lecture. This will help you stay organized as we go through the course. We'll create a directory called lec01 inside a cmsc14300 directory in your home folder, and change to that directory. Run the following commands:

mkdir -p ~/cmsc14300/lec01
cd ~/cmsc14300/lec01
Notes: the -p flag in mkdir creates the parent directory if it doesn't exist, and doesn't throw an error if the directory already exists. The cd command changes your current directory to the newly created lec01 directory. The ~ symbol is a shortcut for your home directory. It's a neat way to avoid typing out the full path to your home directory, and it works across different operating systems and users.

Start a Text Editor

Next, let's open a text editor to write our C program. I'd like to try using a pure terminal-based text editor called vim. It's a powerful editor that is widely used in the programming community, and it will be useful to learn how to use it for this course. To open vim, run the following command:

vim hello.c

This will create a new file called hello.c and open it in the vim editor.

The program

In vim with hello.c open, press i to enter insert mode, then type the following code:

#include <stdio.h>

int main(void) {
    printf("Hello, world!\n");
    return 0;
}

When done, press Esc to exit insert mode, then type :w to write the file. Before we exit vim, let's take a moment to understand what this code does.


Line by line

  • #include <stdio.h>
  • A preprocessor directive. It pulls in the standard I/O header, which declares printf. Without it, the compiler doesn't know what printf is.
  • int main(void) { ... }
  • The entry point. Execution starts here. It returns an int (the program's exit status), and (void) means it takes no arguments.
  • printf("Hello, world!\n");
  • Prints text to the screen. \n is the newline escape sequence - it moves the cursor to the next line. Note the statement ends with ;.
  • return 0;
  • Hands control back to the operating system. 0 means success; a non-zero value conventionally signals an error.

Compile and run

C is compiled: we turn source code into an executable, then run it.

$ clang hello.c -o hello
$ ./hello
Hello, world!
  • clang hello.c -o hello - compile hello.c into an executable named hello (the -o flag sets the output name; without it you get a.out).
  • ./hello - run the executable in the current directory. The ./ says "look right here," not on the system path.

Common first errors

Symptom Likely cause
expected ';' after expression Missing semicolon at the end of a statement
implicit declaration of function 'printf' Forgot #include <stdio.h>
clang: error: no such file or directory: 'hello.c' Wrong filename, or you're in the wrong directory
bash: ./hello: No such file or directory Compile step failed (or wrong name) - no executable was produced
Nothing prints on its own line Forgot the \n in the string

Read the error messages. The compiler usually tells you the file, line, and column - start there.


Try it yourself

  1. Change the message to greet yourself by name, then recompile and run.
  2. Add a second printf so the program prints two lines.
  3. Remove the \n from the first printf and rerun - what changes?
  4. Delete a semicolon on purpose, recompile, and read the error. Put it back.