C: Hello World!

We’ll start with the most basic C program you can write: a simple “Hello World!” outputted to the screen.  I’ve included several screen shots in this tutorial, which you’ll need to click to enlarge them to their full size (I doubt you can see them properly otherwise).

Writing the code

Open up C-Free and you’ll be presented with an all-but-empty screen, with a menu or two along the top.

Create a new file either by going to File and then New, or clicking the new file icon in the top left corner.  Or you could just Ctrl + N.

You’ll now have blank white page, and you should notice that your blinking text cursor is position next to the number 1.  This is the line number, which you don’t need right now, but will do in the future.

So now you can start typing out the code:

#include <stdio.h>

int main()
{
	printf("Hello World!");

	return 0;
}

The blank lines aren’t technically necessary, but it’s good practice to break up your code like this to make it more readable- especially when it comes to hundreds or thousands of lines.  Similarly, the indents aren’t mandatory, but it’s good practice.  And besides, a lot of code editors- like this one- put the indents in automatically.

So once you’ve finished typing that out, your screen should look something like this.

Now, if you look up towards the top-left of the screen, you’ll notice that our file’s called Untitled1.  And since we haven’t yet saved it (that’s what the asterisk means), let’s do so.

Saving the file

Go to File and Save, use the save icon or do Ctrl + S and the save window pops up (right).  Firstly you need to decide where to save it. I’m just sticking with the default location for now.

Next, you need to call it something.  The whole point of a file name is to briefly describe its contents, so that you know what it is without opening it.  So don’t call it “program 1″- call it something descriptive.  When naming files, avoid using spaces, and use underscores (_) instead.  Secondly, try not use you capital letters- just stick to lower case- it’s convention and good practice.

Finally, you’ll probably need to change the file type.  The drop-down box will give you a list of four or five options: you should chose ‘C Files(*.c)’, since we’re coding in C.

Compiling

The compiler turns the human-readable code that we’ve just written into something that the computer can understand: binary.  It’s all automated, however, if you’ve made a mistake in your code, it will fail and you’ll have to go back and correct it.

So now go to Build and then Run, or alternatively just hit F5.

After a second or two, a black window (the Command Prompt) will appear, and if you’ve followed the instructions, it will look like this.

Congratulations- you’ve just created and ran your first program in C!

Tweaks and fixes

You’ve probably noticed that after your “Hello World!”, there’s some other text on the same line.  This doesn’t look particularly nice, but it’s easy to fix.

Close the window so that you’re back at your code, and modify it by adding \n just after the exclamation mark, like so:

#include <stdio.h>

int main()
{
	printf("Hello World!\n");

	return 0;
}

Now run the program again (F5)- which also re-saves your code in the process- and you’ll now see that the next is on two separate lines.

You can use \n however many times you like.  If you want a black line between the two lines of text, just do \n\n.  If you haven’t already, go ahead and change “Hello World!” to whatever else you like.  By using \n you can display as many lines of text as you want.

Further reading

  • “Hello World!” explanation: This is some optional, but nevertheless highly recommend extra reading.  It explains the code that we’ve just written line-by-line, and it’s not only useful to understand, but interesting as well.
This entry was posted in C programming. Bookmark the permalink.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>