Your First C Program

This is a pair exercise and must be competed in your tutorial or lab with your partner.

You should make sure you have completed learning linux before completing this task.

Now it’s time to create a file. We are going to use the text editor gedit

Type:

$ gedit bad_pun.c &

This will open up a graphical editor where you can manipulate text.

Adding the & to the end of a command allows the GUI to return control to the terminal as soon as it starts (i.e. run in the background) rather than after it is closed. If you forget the &, you will not be able to type commands into the terminal until the GUI application is finished. This is always a good idea when running a command which invokes a graphical user interface (GUI).

Here’s an example of a simple C program. Copy it into the gedit window, and save it.

// A simple C program that attempts to be punny
// Written 23/2/2017
// by Angela Finlayson (angf@cse.unsw.edu.au)
// for COMP1511 Lab 01 Exercise 1

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[]) {

    printf ("Hello, it is good to C you!\n");

    return EXIT_SUCCESS;
}

When you save, it will place the contents of the editor into the file bad_pun.c.

File Comments

Every C file you write will start with a comment like the one above. It contains the following important information:

  • Your name and zID,
  • Your tutor’s name and the name of your class,
  • A brief description of the file and program,
  • The date that you wrote the code on. Be clear with the date, we suggest a format of YYYY-MM-DD (for example 2017-07-24), or DD Mmm YYYY (24 Jul 2017), so that there is no confusion regarding American-style dates.

It should look something like this:

  // A description about the C program
  // Your name (your zID), your partner's name (their zID)
  // Your tutor's name (your tutorial code)
  // Date you wrote the program

  #include <stdio.h>
  #include <stdlib.h>

  int main (int argc, char *argv[]) {
      // Your code goes here

      return EXIT_SUCCESS;
  }
Handy Tip 1

On many linux systems copying can be achieved by simply highlighting with the left mouse button and pasting can be achieved by simply clicking with the middle button

Handy Tip 2

Make sure gedit is displaying line numbers down the left hand side. This is important for when we need to fix compile time errors. If it is not displaying line numbers, go to the Edit -> Preferences menu item and check the ‘Display Line Numbers’ option.

Once you have pressed saved, click on the Terminal window again and type this command to compile your program:

$ dcc -o bad_pun bad_pun.c

The -o bad_pun option tells the compiler to give the newly compiled program the name bad_pun.

Test what happens if you leave the -o bad_pun option out. What is the default name dcc uses? (remember, you can use ls to see what files are in the directory.)

You may wish to use the rm command to delete (remove) the file created. (You can Google rm or rm linux if you are not sure how it works.)

If dcc does not print out any error messages, then your program has been successfully compiled. Otherwise you will need to find the errors in your code, fix them, save the file and compile again.

Handy Tip 3

Look for the line numbers that are displayed in the error messages as they are major clues to where the problem is in your code.

Handy Tip 4

Always start with fixing the first error first. Sometimes fixing one compile error, saving and recompiling can make all or some of the other errors go away!

After successfully compiling you can check that dcc has produced an executable by typing ls -l and looking for a newly-created bad_pun file (check the file creation time to see if it really is new).

A useful Unix command is man short for manual. Find out what the -l option for the ls command you used before does. Type:

$ man ls

Press ‘q’ to exit the man program.

Run the program to test that it works. Type:

$ ./bad_pun

The ./ before the program name specifies that the program is found in the current directory. Note that Unix executable (program) names do not require the .exe extension that you might have have seen under Windows.

For COMP1511, we have some tools to help you solve these lab exercises: Autotest, and Styl-o-matic. Autotest runs your solution through some automated tests, and reports back how your code went:

$ 1511 autotest bad_pun
Test 1 (./bad_pun) - passed
1 tests passed  0 tests failed

Styl-o-matic checks your code style and will tell you if your code doesn’t comply with our style guide. In the future, you won’t be able to submit without passing Styl-o-matic.

To run some simple automated tests:

$ 1511 autotest bad_pun

To run Styl-o-matic:

$ 1511 stylomatic bad_pun.c
Looks good!

You’ll get advice if you need to make changes to your code.

Submit your work with the give command, like so:

$ give cs1511 wk01_bad_pun

Or, if you are working from home, upload the relevant file(s) to the wk01_bad_pun activity on Give Online.