Getting Started With C Programming – Hello World Tutorial

If you’ve always wanted to dabble in C, but never quite gotten around to it, have a read through this and give it a go.

It is much easier than you might think (easier now, in fact, than it ever has been), to write your first C program, and there is something so deliciously inviting about the language that once you get started you’ll soon be solving world issues with your code.

To follow along with me:

You will need

  1. A computer running a Linux variant (I use Fedora)
  2. A compiler
  3. This walkthrough

Why do I need to run Linux, can’t I just do this on Windows? Why do you always do everything on Linux??

Well. I’m not going to get into a debate about which operating system is the best. After all, I use a Mac in my day to day life.

Listen: Linux is easy to try out. You can run a live CD (just pick up a copy of Linux Format and use the CD they provide, or download and burn one that you fancy trying: Fedora, Ubuntu, or Mint are all nice), and it won’t even touch the data on your Windows/Mac. Some of the world’s most powerful systems run on Linux-style platforms and as a coder, you are missing out if you never venture to try it.

Hmph. OK I’m in. 

Excellent.

We’ll do this in five steps:

  1. Install a compiler
  2. Write a program
  3. Compile and run the program
  4. Discuss the code
  5. Talk about the compiler

Step 1

First of all we need to install a compiler. The compiler is the thing that takes your code and turns it into something that the computer will understand (machine language).

Open up a terminal (in Fedora it’s under “Sundries”).

Type

gcc --version

If you get “gcc: command not found” then you’ll need to install gcc. To do this type:

sudo yum install gcc

If you haven’t set up your account to use sudo, log in as root to perform the install:

yum install gcc

It will ask you if it’s okay to install the listed components, so type ‘y’ and hit return. When that completes, try the:

gcc --version

command again, and you should see output like this:

[faye@localhost ~]$ gcc --version
gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-7)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[faye@localhost ~]$

The version number might not be exactly the same. That’s OK.

Step 2

Great! We’ve done the hard part.

Next step is to actually write a program. Now, you have a choice of editors on Linux, and I won’t make you use vim to start with (unless you want to). However, you can’t use a word processing program (like Open Office) either*.

On Fedora, if you want the look and feel of a word processing program, you can use gedit. There are lots of other editors like emacs, and nano, and even full-blown IDEs like Eclipse, but for now, we just want to get started. gedit will do the job for us.

In gedit, type the following:

gedit_hello_world

And save it somewhere (like in your Documents folder) as hello.c

You’ve just written your first program!

We’ll compile and run it, and then we’ll talk about what the code actually means.

Step 3

To compile your program, return to the terminal window. Make sure you are in the directory that your newly created file is in. In my case, as I saved it in my Documents folder, I can navigate there with:

cd /home/faye/Documents

You’ll need to replace ‘faye’ with your username (which is also the name of your home directory).

Now, type:

gcc hello.c -o hello

We’ll talk about what this line means in Step 5.

Run your program with:

./hello

You should see output on your terminal that looks like this:

helloworld_ouput

Congratulations! You’ve just written your first C program, compiled it and run the executable.

Step 4

That’s all well and good, but you need to know what the code means, right?

Let’s go through it line by line.

#include <stdio.h>

This tells the compiler to include some code from a library. The stdio library in fact. When you write code in C, you really don’t want to be doing everything yourself. Instead you can make use of pre-built libraries where the code has been written and tested for you. The stdio library includes functions that deal with the input and output of characters (to files, or the screen, for example). There are lots of functions in stdio, but we are only using one here and it is called printf (you can see that further down in your code).

int main()
{
}

The next part of the program is called the main function.

Every program you write has to have one of these.

It’s the entry point for execution of whatever code you want to write.

It starts with int because that is what the function returns to the operating system when it exits.

Huh?

When you create any kind of function (which is a collection of code that does a single job – in this case, it runs your program), that function has what’s known as a return type. The return type here is a number: int is the keyword for an integer.

That number is picked up by the operating system when your program exits. Conventionally a return value of zero indicates all is well, while a return value of non-zero indicates some kind of error or unexpected state.

You will notice that main has brackets () after it. These are always used after a function name and they are the place to put arguments – i.e. information  – that we might want to pass to a function. Our main function has no arguments.

printf("Hello World!\n");

This line is calling the printf function in the stdio library that we included at the top. Again, the brackets enclose the arguments that we pass to that function, which in this case is a single string (a collection of characters enclosed in quotes).

The printf function sends whatever you pass to it onto the standard output (stdout), which is your terminal.

The eagle eyed among you will notice the \n at the end of Hello World! This is called an escape character. We won’t be looking at those just yet. For now, know that this one in particular tells stdout to start a newline at the end of the string.

Note that the printf line ends in a semicolon. This is true of all programming statements. They tell the compiler that it has reached the end of the line in question. It’s like the computer equivalent of a full stop. You will get a compiler error (not always an obvious one), if you forget to use these.

return 0;

This is the last line, and it’s the one that specifies what the return value should be from the main function. It’s sending a zero back from main to the operating system, which means all is well.

Step 5

When you compile your program, you use this line:

gcc hello.c -o hello

gcc is an executable – a program. You invoke it on the command line with its name. Then, so that it knows exactly what to do, you pass it the name of your file hello.c.

Next, we have what’s known as a compiler option: -o (for outfile). This tells the compiler to name the executable that it generates from your code with the filename you pass to it, i.e. hello.

You could compile with just this:

gcc hello.c

If you did, the compiler would use the default name for the executable it generates, which is a.out. To run your program, instead of typing ./hello you’d need to type ./a.out.

A really important note

One thing that’s really important to hear at this point is that it’s okay to be confused and/or overwhelmed. There is no easy way to learn programming, you just have to dive in.

The problem is, it’s very hard to learn little bits of programming without calling into question all the other aspects of programming. Why is it done like that? What does that mean? Is that always the case? Isn’t there a better way?

There aren’t really any standalone topics that don’t involve at least some other technique/algorithm/aspect of the language.

So the best way to learn is to accept that you won’t make sense of everything in the beginning, but when you come back to the beginning at some point in the future, you will see that you have learnt a lot more than you realised.

Write code, and do it often. Read about code, and do it often. Eventually it will all start to make perfect sense 🙂

Action points

  • Write your own hello world program
  • What happens if you remove the escape character: \n ?

 

*Why? Because word processing documents save files as binary with lots of additional mark-up that would confuse our compiler.