In this C Programming course are using tools provided by the Linux operating system. Linux is a variation of Unix.
Although you could install Linux in your computer, we will rely on a computer system the university provides (grace.umd.edu). Anyone registered in this course will have an account on this system. The account id will be your directory id (e.g., terps) and the required password, your usual password. To access grace.umd.edu you need internet access and a program (ssh client) that will establish a connection between your computer and grace.umd.edu. Establishing a connection will create a window on your computer where you can execute commands.
Using an ssh client (see Logging on / Logging off to grace.umd.edu) log on to grace. When you log on you will land in the home directory (a directory is another name for folder). In this directory you can create files and directories. A prompt (e.g., grace2:~:) will appear after which you can execute commands. You execute commands by typing the command and pressing enter. To log out you can execute the logout command.
Each time you log on to Grace you need to authenticate using Duo (e.g., your phone). By using a VPN connection (see UMD VPN) you can prevent multiple authentication requests. First, access the school VPN and then log on to Grace as described above. Instructions on how to install VPN can be found at https://itsupport.umd.edu/itsupport?id=kb_article_view&sys_kb_id=275f52281bc51254d2b564e9bc4bcb67
Linux has tons of commands, but we want to focus on the ones that will allow us to write and execute C programs.
You can create files by using editors (e.g., emacs, vim) or other Linux commands. Initially we will use a simple text editor named nano, so we can focus on creating files.
To create a file using nano, type nano followed by a file name (e.g., nano p1.c). If the file already exists, the file will be opened, otherwise it will be created. Once you open a file, you will see at the bottom a commands summary. The ^ represents the CTRL key. Once you have opened the file, you can just start typing and use the arrow keys to move around. To save and exit execute ^X. At this point try creating a file with the following contents:
#include <stdio.h> int main() { printf("I like coffee\n"); return 0; }
To compile a C program, you will use the gcc command. To compile the program p1.c you created earlier, execute the command gcc p1.c. If there are errors, you will see a listing of those errors; otherwise you will get the prompt back. If there are no errors, the compiler would have created a file called a.out. If you type a.out at the Linux prompt, the program will be executed. The file a.out is overwritten each time you compile. It represents the machine language equivalent of the C program. Unlike Java programs where you compile to bytecode, and that bytecode can be executed by any java virtual machine, C code compiled for an environment will not necessarily work on others (you need to compile again). At this point you should compile p1.c and introduce errors to see the messages generated by the compiler.
Several gcc compiler options are available that can provide additional warnings, generate code with different levels of effciency, etc. For example, gcc -Wall p1.c will provide additional warnings during compilation. For this C Programming class we have defined the options you need. To avoid having to type all those options each time you compile, you can create an alias for gcc. After you setup your account (see below) try setting the gcc alias. Information on how to set the alias can be found at setting gcc alias.
Start exploring linux commands by practicing the commands below. Make sure you are in your 106 directory (if you are taking CMSC106) and in your 216 directory if you are taking CMSC216.
Once the semester is over, make sure you reset your Grace account/environment as described at Resetting Grace Environment
set prompt="[%m %~: ] "%m stands for the machine (e.g., grace1), %~ represents the current directory. Here is how the prompt will look like if we were in the tmp directory. Other options to try: %M, %n.
[grace4 ~/tmp: ]