CSC 2400 Activity Compiling and Running Programs in Unix


Activity Steps

  1. Login to one of the department's Unix or Linux machines (tanner, felix or helix), or to the terminal on a Mac or a Linux virtual machine if you have one already installed.
  2. In your home directory you should see a directory called systems (if you do not, then create one). Change your current directory to systems, then clear up the entire contents of this directory by typing in at the shell prompt
        rm -rf ~/systems/*
    The symbol '*' is a wild character that stands for "everything", so we remove everything (files and directories) located in the systems directory.

  3. In your systems directory, create another directory called digitsum.
  4. Change your current directory to systems/digitsum. Invoke the emacs editor to write a program called digitsum.c using the command
        emacs digitsum.c
    Remember to use Ctrl-x Ctrl-s to save your file, Ctrl-x Ctrl-c to exit, and Ctrl-g to cancel a partially typed or accidental command. Save your code frequently.

    Using the editor, write C code for a program that reads in a positive integer and prints out the sum of all digits. For example, if the user types in the number 528, the program should output 15. Here is a sample program execution (user input is in italics):

        Please enter a positive integer: 345
        The sum of digits is 12
  5. To compile your code, type at the shell prompt the command
        gcc digitsum.c -o xdigitsum 

    gcc (which stands for GNU Compiler Collection) is the name of the compiler. The first argument digitsum.c is the name of the C program you wish to compile. The option o informs the compiler that the argument to follow (in our case xdigitsum) is the name of the executable program to be produced as result of the compilation. You can give any name you wish to the executable, however including your program name in the executable name (without the .c extension) is a good rule of thumb, so you can easily associate an executable with the corresponding source code. Pay careful attention to error and warning messages, and eliminate them all.

  6. To execute your program, use the command
    So you invoke the executable by its name, but you need to specify the entire path (in this case, relative to your current directory) for the executable.

This activity will not be graded.