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 csc2400 (if you do not, then create one). In your csc2400 directory, create another directory called digitsum.
  3. Change your current directory to csc2400/digitsum. Invoke the pico editor to write a program called digitsum.c using the command
      pico digitsum.c 

Notice the menu at the bottom of the editor window that reminds you how to save your code with CTRL-O, how to move between pages with CTRL-V and CTRL-Y, how to cut and uncut a line of text with CTRL-K and CTRL-U, and how to exit with CTRL-X. Remember to 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
  1. To compile your code, use the command
      gcc digitsum.c -o digitsum 

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 digitsum) 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 keeping the executable name the same as the program 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.

5.     To execute your program, use the command

  1. Demonstrate your program and have the TA or instructor initial here: _______ This activity will not be graded.