Recent Changes - Search:

TEAMS Academy Wiki

THS 2011-2012

Explore TEAMS!
for visiting sophomores & juniors

Bodies & Bones

Interactive Robotics

Environmental BioTech

Bat Engineering Design

Assistive Technology & Electronics

Students (Forum)

Instructors

School Calendar

FY09 Planning

TEAMS Web Site

Wiki Info

edit SideBar

Examining a Simple Program: Hello World

Notice
This example program is written for a PC, and uses the printf function, which is used to print text to the screen. There is a similar function for the robot, called cr8_printf, which prints messages over the USB cable that can be viewed with RealTerm.

Let's take a look at a very simple program, and pick out the important basic parts of it. The program below is called the Hello World program, and is often the very first C program code that people see. It does just one thing: print out "Hello World" on the screen, and then exit.

// Preprocessor Directives section
#include <stdio.h>

/* main
   This program will print out a message and exit.
*/
int main(void)
{
   printf("Hello World!");

   return 0;
}

Lets look at lines 1 and 2:

// Preprocessor Directives section
#include <stdio.h>
The first line is called a comment. We mark text as a comment by putting the two slashes (//) before it. Any text following the two slashes will not be compiled. You can use comments to explain to other people what your code is doing, or to help organize your code (which we are doing on line one), or even to remove a line of code you think is causing an error, but don't want delete (because the code might be good, and you don't want to forget it). This comment marks the preprocessor directive section, but what is a preprocessor directive?

The second line is a preprocessor directive. Preprocessor directives are marked with a leading pound symbol (#), and they span the entire line. Preprocessor directives are used by the first part of the compiler, called the Preprocessor, and generally help organize the source files and prepare them for compilation. This particular type of directive is the include directive, and you may recognize it from the wiki. It takes all the text of another file, copies it, then inserts it where the #include is. In the Hello World program, it inserts the text of a file called "stdio.h". Do you recognize what type of file this is? It's a header file, which means it is 'introducing' this Hello World program to another source file. The name "stdio" stands for the C "Standard Input/Output" Library, which contains many standard functions for performing input and output with your program, and is part of the bigger C Standard Library. We need to introduce Hello World to the standard I/O library, because we use a standard library function later in the code in order print the text "Hello World".

Now, lets look at lines 4 through 7:

/* main(void)
   This program will print out a message and exit.
*/
int main(void)
The first three lines are another type of comment, which allows multiple lines of text to be marked as comments. Block comments begin with a slash-star pair, and end with a star-slash pair. This block comment tells anyone who reads the code what the program will do. Using comments this way allows you to document your code, which is an important coding practice. Good documentation helps people (including yourself) understand your code, and can help locate logic errors.

Line 7 is the function header of the main function, which is always the first function to run in your program. In math, a function takes an input and produces an output; C functions do this as well, although they can be a little more flexible. To the left of the word main, there is the word int, and to the right, there are open and close parenthesis around the word void. Inputs to a C function are put between the parenthesis, and the output type of a C function is placed to the left of the function name. Our main function takes the input "void", which is another way of saying nothing, and its output type is "int", which is the C keyword for "signed integer". But wait, if this program is supposed to print out "Hello World", why is it's output an integer? Let's put that question on hold for now, and see what the main function does.

Lines 8 through 12 make up the body of the main function:

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

   return 0;
}
On line 8 there is an opening curly brace ({), and on line 12 there is a closing curly brace (}). Anything between these braces is inside the main function, and can also be called the scope of the main function. Scope will be discussed later. When the computer executes a function, it starts at the very top, goes step by step, line by line, through the function. The first line of code after the opening curly brace is:

printf("Hello World!");

This is a call to the "printf" function, which can be used to print text to the screen. It's input is the text string "Hello World!". The output is the number of characters it was able to print to the screen, but since printf is on a line by itself, the output is not saved, and it disappears. The text "Hello World!" is still printed to the screen though. When a function does something besides produce an output, it is said to have a side effect, and in this case, the printed text is the side effect. This is why the output type of main can be an integer: the printing of text is a side effect. To see the printf and cr8_printf reference page, click here.

The line of code after printf is how main produces it's output. The return keyword is required for any function that produces a value as output. Basically, it tells the function when to end. The Hello World program is finished after the main function executes the return statement. If any other programs are waiting for the output of the Hello World program, they will get a value of zero. The standard practice for C programming is to have main return zero when it finishes with no detectable problems. If errors have been detected and main can't proceed because of it, then it could return a unique non-zero number (an error code) for that specific error. This way, a person debugging the program will know where the program went wrong.

In summary, our Hello World program:

  1. Contains documentation, using comments, to help us organize and understand the code.
  2. includes the Standard I/O Library so it could use the printf function.
  3. Starts with a main function that does use any inputs and produces an integer as output.
  4. Calls the printf function to print text to the screen.
  5. Stops by returning zero, which means it completed without detectable problems.

Hello World from the Create

Here is the Hello World program above, rewritten for the Create.

// Include the Create library
#include "create.h"

// Declare the Sensor Data Structure
cr8_t *cr8;

/* main
   This program will send a message over USB, 
   which can be viewed with RealTerm
*/
int main(void)
{
   // Initialize  the Create and Sensor Data Strucutre
   cr8 = cr8_alloc();
   cr8_init(cr8);

   // Send message over USB
   cr8_printf("Hello World!");

   // Free Sensor Data memory
   cr8_free(cr8);
   return 0;
}

Introduce cr8 stuff


<< Files | Home Page | Syntax >>


Have a Question? Please ask below.

Edit - History - Print - Recent Changes - Search
Page last modified on January 18, 2008, at 12:18 PM