1. Computing

Introduction to C

Hello World

By

This is the first tutorial in the series Online C Tutorial for beginners. The next tutorial introduces variables and a way to count with them.

This is a rewriting of the original C tutorials into single page tutorials which can usually be run on either codepad.org or ideone.com.

Note these online services and others are provided free by third party providers so please don't abuse them. I've used them because they save you the hassle of installing a C compiler. You might consider doing that and running them instead because with many users hitting these services the response may get slow.

For the file tutorials, you will definitely need to install a C compiler and I'll cover that in my next C tutorial.

Let's Start with Hello World

Because it's a tradition, but not a bad one. It shows off a minimal subset of the language features and is easy to explain.

For Absolute Beginners

Developing C programs is done by editing a text file that has a .c extension. So I might store this program in a file called hello.c.

Then I run it through a program called a compiler that reads it and translates it into code that the computer can run directly. If I have a mistake in the program, then the compiler will give a compile error and flag the error. So I have to edit it, fix the error and recompile it.

The online services provide an editor, compiler and let you run the program all in one. This is convenient and much easier, not to say faster than installing a compiler, then learning the commands to compile and run it. But not all programs can be run this way.

/* Hello world, By David Bolton http://cplus.about.com */
#include <stdio.h>

int main(int argc, char * args[]) {
  printf("Hello World");
  return 0; }

Running it on Ideone

Open up ideone.com in your browser and paste this Hello World link. It shows the source code in the middle and the result of the run. You can copy the source code to the clipboard using the copy to clipboard command or even clone it and play with it in ideone.

Line by Line explanation of Hello World

Here is the program shown with line numbers and an explanation of each line.

1. /* Hello world, By David Bolton http://cplus.about.com */
2. #include <stdio.h>
3.
4. int main(int argc, char * args[]) {
5.   printf("Hello World");
6.   return 0;
7. }

  1. This is a comment. The compiler ignores everything between /* and */. For every /* there must always be a corresponding */. Newer C compilers allow you to add a comment on a single line by starting it with //.

  2. The #include is called a compiler directive and tells the compiler to include a standard c library called stdio.h. Inbuilt C libraries are always inside angle brackets < > but libraries you create should be side double quotes "like this".

  3. You can use any amount of blank lines to make your program easier to read.

  4. All C programs are made up of functions and the one called main is the one that is always run. Everything from this line to line seven is part of main. The int tells the compiler that main will return an int value and that's done in line 6. Ignore everything in the brackets for now, we'll come back to that when we look at function parameters. The code starts with the curly brace { on the end of this line and ends with the matching closing curly brace } on line seven.

  5. The printf function is called to print out the text string "Hello World". Text strings are always give inside double quotes "Like this". The semi-colon identifies the end of the statement.

  6. This provides the return value from the main function. Again there is semi-colon to terminate this statement.

  7. This is the closing brace } of the main function (and also the program)

So all this does is output
Hello World.

Program Architecture

The compiler knows that when you include libraries, it has to build in a copy of the library (or at least some of the functions you use) into the program. The printf function comes from the stdio library and by including stdio.h (the .h file extension means this is a header file containing the information about which functions you can use) the compiler can figure out what values can be used with printf.

The library code for printf has already been compiled and is supplied as an object code file (code that the computer directly understands) and the last part of compiling that takes place is to take your code, any library code needed (like printf) and build it all into one exe that your operating system (Windows, Linux or Mac OS X) can load into memory and execute. This is known as linking but only happens if your code compiled successfully.

  1. About.com
  2. Computing
  3. C / C++ / C#
  4. C Programming
  5. C Tutorials
  6. Online C Programming Tutorial for Beginners - Lesson One

©2014 About.com. All rights reserved.