Skip to content

Instantly share code, notes, and snippets.

@rszewczyk
Last active October 17, 2017 05:40
Show Gist options
  • Save rszewczyk/859f41e810276e8e49f5cd7e28946d2d to your computer and use it in GitHub Desktop.
Save rszewczyk/859f41e810276e8e49f5cd7e28946d2d to your computer and use it in GitHub Desktop.
UMBC CMSC 104 - Fall 2017 - Class 4 - 9/14/2017

In class assignment - 9/14/2017

Create a C Source Code File

Open your editor and choose the option for creating a new file. Paste the contents below into the file:

#include <stdio.h>

int main() {
  return 0;
}

Save the contents as a file with the name hello.c. Here's what it looks like in Code on macOS.

saving a source file in Code

Upload the Source File

Open Filezilla. In the right side file explorer, navigate to where you saved the file in the previous section. Open the Site Manager and connect to linux.gl.umbc.edu. Once you've established a connection, use the right side window to create a directory called cmsc104 (unless you already have one). Double click that directory to enter it, then create a directory called class4. Double click the class4 directory. Then double click your hello.c file to transfer it to the remote server. Do not disconnect or close Filezilla.

Here's what it looks like in macOS:

upload file with filezilla

Compile and Run on the Remote System

Use PuTTY (Windows) or Terminal/SSH (macOS) to log into the UMBC Linux server. Then do the following:

  • Type cd cmsc104/class4 and press enter to change to the directory where you uploaded hello.c.
  • Type ls and press enter. You should see hello.c as part of the directory listing.
  • Type gcc hello.c and press enter to compile the source file into a runnable program.
  • Type ls and press enter. You should now see a file called a.out in the list. This is your compiled program.
  • Type ./a.out and press enter to run the program. You won't see any output and it will appear to have done nothing - this is fine.

Here's what the above steps look like in macOS:

compile hello

Update the Program Source and Recompile

Go back to your editor. Let's update the program to print something to screen. Add the line printf("Hello, World!\n"); just above the line with the word return. Your program should now look like this:

#include <stdio.h>

int main()
{
    printf("Hello, World!\n");
    return 0;
}

In Filezilla, double click the hello.c file. If prompted about overwriting the file, go ahead and overwrite it. Then return to your shell (PuTTY or Terminal) and rerun the commands to compile and run the program:

  1. gcc hello.c
  2. ./a.out

This is what it looks like in macOS:

updated and recompile the program

Experiment

Try making some changes on your own to the program, save it and recompile/rerun on the remote system. Can you make it print a different message? How about another line of text? Something more complicated? If you get stuck you can always paste the original program and reupload it and try again. The point here is to get used to the workflow of making small changes to the program, one at a time, uploading them and then trying them out.

Class Objectives - 9/14/2017

At the end of today's class you will be able to:

  • Use an editor to create a C source code file
  • Work with files and directories on a remote system. You will:
    • move files from your computer to a remote system
    • create and rename directories on a remote system
  • Use the cd command to navigate directories in Linux
  • Use the ls command to view the contents of a directory in Linux
  • Compile a C program using the gcc command
  • Run a program compiled from a C source file in Linux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment