C for Java Programmers: Hello World

George Ferguson

Summer 2018
(Updated Summer 2020)

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License
Creative Commons License

In this lesson, you will create and run the basic “Hello World” program in C. You should be familiar with the “Hello World” program in Java, and it's very similar in C. The goal of the lesson is simply to make sure that your development environment is ready to go.

The choice of development envrionment is something of a religious issue among programmers. Some swear by one tool; others think that’s the worst thing ever. You basically have two choices. Either you use an “Integrated Development Environment” (IDE), or you use the command shell.

If you want to use an IDE, you have some choice depending on what platform you are developing on (and for). The main contenders are:

We’ve also heard good things about: FWIW: Wikipedia has a chart comparing C/C++ IDEs.

Unfortunately, installing and using any of these tools is beyond the scope of this tutorial. However if you want to use Eclipse for C development, check out our companion guide Eclipse for C Programming (in CSC173).

Using the command shell is something You Should Know if you are a Computer Science student. Unfortunately, you are on your own until we get around to writing a tutorial. But there are plenty of tutorials on the web. You should be able to find something that works for you. We also recommend the book The UNIX Programming Environment by Kerhighan and Pike. It is a classic and very readable. Try it!

Now on with the lesson...

Create a new file in your IDE or text editor named “hello.c” (without the quotes).
Type (or cut-and-paste) the following text into the file and save the file: /* * File: hello.c */ #include <stdio.h> int main(int argc, char *argv[]) { printf("Hello world!\n"); }
You should be able to build (compile) the program using the IDE or the command shell. If you copied it properly, it should compile without errors. If so, you should be able to run the program using the IDE or from the command shell. Congratulations! You’ve written your first C program.