This tutorial introduces you to C programming under UNIX. It is assumed that you have a terminal session open on a UNIX server, e.g. agave.students.itee.uq.edu.au. This tutorial is a UNIX variant of the original CSSE1000 C Programming Tutorial 1.
It is assumed that you have some knowledge of UNIX, e.g. how to run programs and make and change directories etc. See the UNIX resources for more details.
It is also assumed that you are familiar with a UNIX based text editor, e.g. vi or emacs. See the Editor resources for more details.
The following textbook sections are an introduction to C. It is suggested that you read these, or some other introduction to C.
For these tutorials, we won't be using a development environment - we'll be entering a C program into a text editor and then running the compiler on the command line. You may want to create (and cd into) a directory specifically for working on this tutorial.
Start a UNIX text editor (e.g. vi or emacs), editing the file helloworld.c and enter the following program:
/* My first C program */ #include <stdio.h> int main() { printf("Hello world\n"); return 0; }
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 */
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. 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. Every program must have a function called main() defined somewhere within it. 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 to the operating system 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 the function is demarcated by curly braces {}. The definition starts immediately after main() and finishes two lines later. The lines of code within the curly braces of a function definition are called the function body. These lines are called statements. This function has only one statement in its function body:
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 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.
return 0;
The program was successful so we return status code 0.
Note that statements finish with a semicolon.
Compilation is the process of turning the program source code into an executable program (1's and 0's that the CPU will understand). We're going to use the GCC compiler on agave. Run the following command:
gcc -o helloworld helloworld.c
This compiles the file helloworld.c and produces an executable output file called helloworld. (Without the -o helloworld options, the default name of the executable would be a.out.) If you made an error in the program, you would see error messages indicating the problem. You should edit the file again to fix these and recompile. If you have trouble interpreting an error message, please ask your tutor for help.
You should now be able to run the program you have created with the command
./helloworld
(The ./ part of the command, means to look for the executable in the current directory.
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");
The following program is an example of one which gets some numbers from the user and adds them up - it continues until you enter 0. Try typing it in (or cut and paste it in) and compiling and running it to see what it does.
#include <stdio.h> int main() { int num; /* Declares an integer variable to hold the number read in */ int sum; /* Declares an integer variable to hold the sum */ sum = 0; /* Initialise the sum */ do { printf("Sum so far: %d\n", sum); printf("Enter number: "); scanf("%d", &num); /* Read in an integer from the user */ sum = sum + num; /* Add num to sum */ } while (num != 0); printf("FINAL TOTAL: %d\n", sum); }
You don't need to understand all of this program yet - some of these elements will be covered by future tutorials. This is just an example of a slightly more complex C program. Can you modify it so that it only stops when a negative number is input?