Introduction to C programming/Hello World

From Wikiversity
Jump to navigation Jump to search

If you have not installed the applications cited in the Software Needed section, please do so before moving on!

Hello World[edit | edit source]

The first application that almost every programmer begins with is the "Hello World" application. This is a very simple application that only prints a line of text to the terminal window. This lesson will demonstrate the basic principles and techniques that are used when designing more in-depth applications.

The process is simple: open your text editor, (emacs, Xcode, etc...) and type in the following text:

  #include <stdio.h>
  int main ()
  {
    printf("Hello World!\n");
    return(0);
  }

Next, save your text file as "hello.c" or something similar. It is very important that the file has the extension ".c" instead of anything like ".doc" or ".txt".

Now we can compile the program using gcc within a terminal. Navigate to the directory where your file is saved and enter "cc hello.c". If all went well, you should see no messages at all. If you do, the compiler will tell you what type of error it encountered or what line the error is located in your program.

Assuming that all went well, you should see a new file called "a.out" in your working directory. This file is your first application binary! To run the program, simply type "./a.out". You should see "Hello World" printed into the command line.

You can now use standard UNIX commands to re-name your binary to something like "hello" instead of "a.out".

That's all there is to it!

--Mashedmeat 04:37, 11 March 2009 (UTC)