1. Introduction - Hello World
This tutorial introduces you to the development environment and a simple C program (which just prints a message). The necessary software is present in 47-104, and it is also available for you to download and use anywhere (subject to the terms of the license agreement - it is free for non-commercial use).
Assumed Background
It is assumed that you know how to navigate around (explore) Windows folders (directories) and that you know how to open files within those folders (e.g. by double clicking).
Associated Reading
The following textbook sections are an introduction to C. It is suggested that you read this, or some other introduction to C.
- Kernigan & Ritchie (2nd ed.) - first few pages of Chapter 1
- Harbison & Steele - first chapter.
An online C reference by Martin Leslie is available. The following sections are relevant:
There is also an Introduction to C Programming by Rob Miles (94 pages, 371kB PDF)
Starting the Development Environment
Before beginning the tutorial, it is necessary to start the development environment. We will be using a tool called Programmers Notepad, both as an editor (i.e. a tool to type in the program code) and as a front end to running the compiler (the tool which converts the C program file into an executable program). In this tutorial we will be using a compiler which compiles programs into PC executables. Later in the semester we will use a different compiler with Programmers Notepad to compiler programs for an AVR microcontroller.
Start Programmers Notepad 2 by navigating as follows:
Start Menu -> Programs -> lcc-win32 -> lcc-win32
This starts the editor (wedit). You should see a window that looks something like this:

The first step in writing a C program is to create a new project. From the File menu, select New -> Project ... and fill in the dialog as shown below (you can choose another working directory if appropriate, but it is better if this is on your H: drive or within your Desktop (which gets copied to your H: drive when you logout). Such a location will ensure that you can access the resulting files from any ITEE student lab computer. (If you don't already have a directory named csse1000 in your H: drive, you will need to create it first. The sources working directory specified below should be a subdirectory of an existing directory. (The default output directory is the lcc directory within the working directory - this is fine.) Make sure you indicate that a "Console Application" is being created.

You should see the popup dialog below. Choose "No".

You will then be asked to add source files to the project:

Add a file called "helloworld.c":

This will be the only source file in the project, so click OK when prompted with the "Source files..." window:

You will then be prompted for a number of project settings, first Compiler Settings. Accept all the defaults, except for "Language extensions" where you should select "Ansi-C only". This will produce code that has maximum portability to other compilers.

Click on the "Next >" button, then accept the defaults in the following Linker settings and Debugger settings windows:


You will then be presented with an empty editor window:

Enter the following program:
/* My first C program */
#include <stdio.h>
int main() {
printf("Hello world\n");
return 0;
}
The result should look like:

The different colours are not part of the program. The editor uses syntax highlighting to differentiate between the different parts of the program (e.g. comments and keywords).
You should save the program (select Save from the File menu).
An explanation of the program
This short program is made up of the following:
/* My first C program */
This is a comment. Comments in C start with /* and end with the first */. Comments can span multiple lines, e.g.
/* This is a mult-line comment */
Comments do not change how your program operates, they are ignored completely by the compiler and program execution. They are there to make the source code easier reading for people.
Note that comments in C can not be nested, i.e. you can't put a comment inside a comment.
#include <stdio.h>
This line tells the compiler to include a header file called stdio.h - which describes functions available in the standard input-output library. A library contains useful functions - in this case the standard input-output library contains a function called printf which we can use to print information out to the screen. It is necessary to tell the compiler about such functions before we use them.
int main() {
C programs consist of variables and functions. This line begins the definition of a function called main. The main function is special - it is the function that is called when your program is executed, i.e it is the program entry point. Every C program must have a function called main() defined somewhere within it. Our example program consists of only a single function called main.
The return type of the function is specified before the function name - in this case "int" i.e. an integer. main() returns a status code (sometimes called an exit code or return code) to the operating system (OS) so the OS knows whether an error occurred or not. A status code of 0 means no error occurred. Other functions will return other types, or "void" if nothing is returned.
The definition of a function is demarcated by curly braces {}. The definition starts immediately after main() and finishes three lines later. The lines of code within the curly braces of a function definition are called the function body. Function bodies are made up of declarations and statements. Our function has no declarations and 2 statements in its function body. The first of these statements is:
printf("Hello world\n");
This statement prints a message that the user sees. It does this using the function printf and passes that function one value (or argument) to work on. (Arguments to functions are listed inside parentheses.) The single argument in this case is a text string: "Hello world\n". (Double quotes are used to enclose strings in C - they won't get printed out.) The \n within the string means newline. The second statement in the function body is:
return 0;
This means the program was successful so we return status code 0. (A return from any other function will return the flow of execution to the function which called it. A return from the main() function will terminate the program.)
Note that all C statements finish with a semicolon.
Compiling the Program
Compilation is the process of turning the program source code into an executable program (1's and 0's that the CPU will understand.) Select "Execute ctute1.exe" from the Compiler menu.
(Note the images below come from an earlier version of the compiler which added ".err" to the executable name.

This will cause the program to be built and then executed. You will get asked whether you want the program to be built. Choose "Yes":

You will get a message when the program has been built:

and a console window will appear, showing you the result of execution:

If you made an error in typing in the program, you would see an error message indicating the problem, e.g.:

You can double click on the error message to move the cursor to the location where the error was detected. Beware that the error may actually be on an earlier line. If you have trouble interpreting an error message, please ask your tutor for help. After fixing the error, you can save the file and rebuild, then execute the program.
After you have run the program, press any key to close the window.
What now?
Try modifying the program to print out other messages. Try adding more newlines and try multiple printf statements, e.g.
printf("Hello ");
printf("World\n");
printf("Again\nand again\n");
Variables and basic Program Input/Output
Variables are piece of data that a program can use to keep track of information over time. Suppose we wish to make a program that accepts two input numbers from the user ("a" and "b") and computes their sum (very basic calculator). We would need two variables to store the input numbers in and a third for the result. Before variables can be used however they must be declared. Variables can be declared at the start of functions using declarations such as:
int main() {
int a;
int b;
int sum;
....
Variable declarations consist of two parts, the variable type
and the variable identifier (or name). "int a;" means I am
declaring a variable named "a" of type "integer". Here we have declared 3
variables "a", "b" and "sum" all of which are integers. Variables can be
assigned using an assignment statement ("=" statement). For example the
statement:
a = 10;
Will set the variable a to the value 10. Integer variables can be set to arithmetic results as well. For example:
a = 10;
b = 5;
sum = a + b;
We would expect the variable "sum" to equal 15 after execution of these
three statements.
We can get our program to output our integer variables to the screen using a the printf function. The statement below will print the sum to the screen with an appropriate message:
printf("My variable equals %d", sum);
The %d sequence in the text string means "take the next argument and print it out as a integer". The d stands for decimal, that is the integer we pass to it will be
printed in base 10. Not the variable "sum" is passed to the printf function along with the
text string. Put all the pieces together into a complete program and observe the
output. The complete program is:
/* My first C program */
#include <stdio.h>
int main() {
int a;
int b;
int sum;
a = 10;
b = 5;
sum = a + b;
printf("My variable equals %d", sum);
}
So we now have variables working as program output, but what about getting some input from the user? A new function scanf can be used to prompt the user to input some integers for us to use (rather than using the hard coded constants 5 and 10). The statement:
scanf("%d", &a);
Will get an integer input (decimal) from the user and put the result in the
variable a. The "&a" means that we're passing a pointer to a as the argument to scanf. We pass this since scanf needs to know where to put the value read from the user. We will further discuss the meaning of the "&" operator in a later tutorial
but it is required to make scanf work. Modify the program to use scanf to
get the input values from the user:
/* My first C program */
#include <stdio.h>
int main() {
int a;
int b;
int sum;
printf("enter an integer:\n");
scanf("%d", &a);
printf("enter another integer:\n");
scanf("%d", &b);
sum = a + b;
printf("The sum of your two integers equals %d", sum);
}
Test out this program (you will want to run it more than once), and observe its operation. Are the output what you expected?
Basic Types and Operators
So far we have only seen variables of "int" type used. Perhaps the type most
interesting type to us in CSSE1000/CSSE7035 is the "char" type. char is like int in that
it represents whole numbers but is limited to 8 bits of data. "char" variables
are signed, i.e. a variable of type char is an 8 bit twos complement number.
Modify the above program to use "char" variables instead of "int":
/* My first C program */
#include <stdio.h>
char scanChar () {
int i;
scanf("%d", &i);
return
(char)i;
}
int main() {
char a;
char b;
char sum;
printf("enter an
integer:\n");
a = scanChar();
printf("enter
another integer:\n");
b = scanChar();
sum = a + b;
printf("The sum of your two integers equals
%d", sum);
return 0;
}
Unfortunately the scanf function we previously used does not natively support the char type for input. We have provided another function in C scanChar to do the job for you in this new example (You do not need to know how this works just yet, just know that with the extra code at the top, scanChar will work like scanf did before for a single 8 bit numeric input).
Recompile and rerun the program and ask it to compute 100 + 105. Is the result what you expected? Convert 100 and 105 to binary and do the 8 bit twos complement addition on paper. Does an Overflow occur and is it consistent with the result of your program?
Question to discuss in your session with your tutor or on the discussion board: How do C programs handle overflow operations?
We can if we wish to modify our variables to be unsigned. That is, our 8 bit numbers are unsigned binary rather than twos-complement. We add the "unsigned" keyword to the type definitions:
unsigned
char a;
unsigned char b;
unsigned char sum;
"unsigned" is an example of several optional type qualifiers in C that allow us to be more specific about how we wish C to interpret our variable types. Modify the program to use unsigned chars instead and rerun the calculation 100 + 105. Does it work?
So far we have only seen addition operations. Other operations are possible a few of which are listed below:
| operator | Example Usage | Description |
| + | sum = a + b; | Addition operator |
| - | diff = a - b; | Subtraction operator |
| ~ | ones = ~a; | Inversion - calculates the ones complement of the operand. |
| | | x = a | b; | OR operator - calculate the bitwise OR of the two operands. Very useful for masking. |
| & | x = a & b; | AND operator - calculate the bitwise AND of the two operands. Very useful for masking. |
| ^ | x = a ^ b; | XOR operator - calculate the bitwise XOR of the two operands. |
| >> | x = a >> 1 | Shift right. Shift the given operand right by a number of bits. The example will shift "a" right by one bit and store the result in x. Note that a remains unchanged. |
| << | x = a << n | Shift left. Shift the given operand left by a number of bits (n in this case). |
As we can see, some of these operator are useful for working with data on the binary level. To make our unsigned chars more binary friendly it is easier to make our inputs and outputs work in hex, rather than decimal. To do this we use the sequence %x in our printf and scanf statements in place of %d. For example the following program will prompt the user for two hexadecimal numbers and output the bitwise AND of the two in hexadecimal:
/* My first C program */
#include <stdio.h>
int main() {
int a;
int b;
int result;
printf("enter an integer (hexadecimal):\n");
scanf("%x", &a);
printf("enter another integer (hexadecimal):\n");
scanf("%x", &b);
result = a & b;
printf("The bitwise AND of your two integers equals %x", result);
}
Enter the number "AF" and "53" into this program. Convince yourself that the program works as expected and that it is generating the correct result.
Finishing up
When you're finished, quit the lcc editor (wedit) by selecting Quit from the File menu.
Make sure you read through the introductory material listed above if you have not already done so.
