Skip to content

Instantly share code, notes, and snippets.

@rszewczyk
Last active February 16, 2017 21:31
Show Gist options
  • Save rszewczyk/1ac754a53b52c05594194edd0b0a4574 to your computer and use it in GitHub Desktop.
Save rszewczyk/1ac754a53b52c05594194edd0b0a4574 to your computer and use it in GitHub Desktop.
UMBC CMSC104 Resources
(global-font-lock-mode 0)
(set-face-foreground 'minibuffer-prompt "white")
(setq c-default-style "ellemtel")

Classwork 2

Note If you do not finish today, you have until the begining of next class to submit this assignment

Objectives

  • More practice using the basic UNIX/Linux commands
  • Practice using the emacs editor to modify an existing C program

Part 1

  1. Make your working directory your home directory: type cd and press return
  2. Verify your working directory is your home directory: type pwd and press return.
  3. Steps 1 and 2 and the resulting output should look something like:
[robsz@linux3 ~] cd
[robsz@linux3 ~] pwd
/afs/umbc.edu/users/r/o/robsz/home
[robsz@linux3 ~]
  1. Make a new directory named Personal: type mkdir Personal and press return
  2. Make your working directory Personal: type cd Personal and press return
  3. Verify your working directory is Personal: type pwd and press return
  4. The last three steps and the resulting output should look something like:
[robsz@linux3 ~] mkdir Personal
[robsz@linux3 ~] cd Personal
[robsz@linux3 ~/Personal] pwd
/afs/umbc.edu/users/r/o/robsz/home/Personal
[robsz@linux3 ~]
  1. Use the emacs editor to create a file called things2do.txt: type emacs things2do.txt and press return
  2. Type the following contents in the buffer
1. Finish today's lab exercise.
2. Finish homework 1.
3. Bring my pet tarantula to CMSC104 for Show'n'Tell
  1. Exit the emacs editor, saving the file as you do so: press Ctrl-X followed by Ctrl-C followed by y
  2. Change your working directory to be one directory up from your current working directory: type cd .. and press return
  3. Verify your current working directory is your home directory: type pwd and press return
  4. The output from the last 5 steps should looks something like:
[robsz@linux3 ~/Personal] emacs things2do.txt
[robsz@linux3 ~/Personal] cd ..
[robsz@linux3 ~] pwd
/afs/umbc.edu/users/r/o/robsz/home
[robsz@linux3 ~]
  1. Start a transcript of your Unix session: type script and press return. The command and the resulting output should looks something like:
[robsz@linux3 ~] script
Script started, file is typescript
[robsz@linux3 ~]
  1. List the contents of the Personal directory: type ls Personal and press return
  2. Print the contents of the file things2do.txt in the Personal directory on the screen: type cat Personal/things2do.txt and press return
  3. The output from the last 2 steps should look something like:
[robsz@linux3 ~] ls Personal
things2do.txt
[robsz@linux3 ~] cat Personal/things2do.txt
1. Finish today's lab exercise.
2. Finish homework 1.
3. Bring my pet tarantula to CMSC104 for Show'n'Tell
[robsz@linux3 ~]
  1. Create a directory called PersonalBackup: type mkdir PersonalBackup and press return
  2. Verify that it exists by listing the contents of your current working directory: type ls and press return
  3. Copy the file things2do.txt from Personal to PersonaBackup: type cp Personal/things2do.txt PersonalBackup and press return
  4. List the contents of the PersonalBackup directory: type ls PersonalBackup and press return
  5. The output of the last 4 steps should look something like:
[robsz@linux3 ~] mkdir PersonalBackup
[robsz@linux3 ~] ls
bin  cs104  Desktop  Documents  Downloads  introtoc  Music  Personal  PersonalBackup  Pictures  Public  Templates  typescript  Videos
[robsz@linux3 ~] cp Personal/things2do.txt PersonalBackup
[robsz@linux3 ~] ls PersonalBackup
things2do.txt
[robsz@linux3 ~]
  1. Attempt to delete the Personal directory: type rmdir Personal and press return. The command and resulting output should looks something like:
[robsz@linux3 ~] rmdir Personal
rmdir: failed to remove `Personal': Directory not empty
[robsz@linux3 ~] 
  1. You cannot remove a non-empty directory. You must first delete its contents. Delete the things2do.txt file in the Personal directory: type rm Personal/things2do.txt and press return then press y to confirm
  2. Verify it is gone by listing the contents of Personal: type ls Personal and press return
  3. Remove the Personal directory: type rmdir Personal and press return
  4. The output from the last 3 steps should looks something like:
[robsz@linux3 ~] rm Personal/things2do.txt
rm: remove regular file `Personal/things2do.txt'? y
[robsz@linux3 ~] ls
bin  cs104  Desktop  Documents  Downloads  introtoc  Music  Personal  PersonalBackup  Pictures  Public  Templates  typescript  Videos
[robsz@linux3 ~] rmdir Personal
[robsz@linux3 ~]
  1. Move things2do.txt from the PersonalBackup directory to your current working directory: type mv PersonalBackup/things2do.txt . and press return
  2. Verify file is in your current working directory by listing the current working directory contents: type ls and press return
  3. The output from the last 2 commands should looks something like:
[robsz@linux3 ~] mv PersonalBackup/things2do.txt .
[robsz@linux3 ~] ls
bin  cs104  Desktop  Documents  Downloads  introtoc  Music  PersonalBackup  Pictures  Public  Templates  things2do.txt  typescript  Videos
[robsz@linux3 ~]
  1. Exit the script session: type exit and press return. The command and resulting output should look something like:
[robsz@linux3 ~] exit
exit
Script done, file is typescript
[robsz@linux3 ~]
  1. Verify that you have a file called typescript in your current working directory: type ls and press return
  2. Submit the typescript file: type submit cs104_kdruffel cw02 typescript and press return
  3. The output from last two commands should looks something like:
[robsz@linux3 ~] ls
bin  cs104  Desktop  Documents  Downloads  introtoc  Music  PersonalBackup  Pictures  Public  Templates  things2do.txt  typescript  Videos
[robsz@linux3 ~] submit cs104_kdruffel cw02 typescript
Submitting typescript...OK
[robsz@linux3 ~]

Part 2

Write a C program that prints out "Hello, world!" twenty times, using a new line each time.

  1. Make your current working directory the introtoc directory that we created as a sub directory of your home directory in the last class: type cd ~/introtoc and press return
  2. If the directory is missing (because you were absent, etc) then make your current working directory your home directory: type cd and press return
  3. Open the file hello.c for editing in emacs: type emacs hello.c
  4. The contents of your buffer should look like:
#include <stdio.h>

int main() {
   printf("Hello, world!\n");
   return 0;
}
  1. If the contents of your buffer do not look like the above, make it so.
  2. Use the cut and paste feature of emacs to edit the contents of the buffer so that your program will print the required line 20 times.
  3. Exit emacs, saving your work. Press Ctrl-X then press Ctrl-C then press y
  4. Start a sript session: type script and press return
  5. Print the contents of hello.c to the screen: type cat hello.c and press return
  6. Compile your program: type gcc hello.c and press return
  7. Run your program: type ./a.out and press return
  8. Exit the script session: type exit and press return
  9. Rename your typescript file to typescript2: type mv typescript typescript2 and press return
  10. Submit your typescript2 and hello.c files: type submit cs104_kdruffel cw02 hello.c typescript2 and press return
  11. Log out of gl: type exit and press return

CMSC 104 - C Coding Standards

General Comments

Every programming department has some set of standards or conventions that programmers are expected to follow. The purpose of these standards is make programs readable and maintainable. After all, you may not be the programmer who maintains your own code more than six months after having written the original. While no two programming department's standards/conventions may be the same, it is important that all members of the department follow the same standards. Neatness counts!!! At UMBC, the following standards have been created and are followed in CMSC 104.

Part of every project grade is based upon how well these standards are followed.

It is your responsiblity to understand these standards. If you have any questions, ask your instructor, the TA, or the Help Center.

Naming Conventions

  • Use meaningful variable names. For example, if your program needs a variable to represent the radius of a circle, call it radius, not r or rad. The use of single letter variables is forbidden, except as simple for loop indices. The use of obvious, common, meaningful abbreviations is permitted. For example, number can be abbreviated as nr as in nrStudents.
  • Begin variable names with lowercase letters
  • Begin function names with uppercase letters
  • Use all uppercase for symbolic constants
  • Use all uppercase for typedefs
  • Do not use global variables
  • Be consistent
  • Separate words within identifiers with underscores or mixed upper and lowercase.
    • 2 word variable name: grandTotal or grand_total
    • 2 word function name: ProcessError or Process_Error

Use of Whitespace

The prudent use of whitespace (blank lines as well as space) goes a long way to making your program readable.

  • Use blank lines to separate major parts of a source file or function.
  • Separate declarations from executable statements with a blank line.
  • Do not use tabs (unless your editor changes tabs to 2, 3, or 4 spaces). Tabs may be interpreted differently by different editors.
  • Use 3 or 4 spaces for each level of indentation
  • Preprocessor directives, function prototypes, and headers of function definitions begin in column 1.
  • All statements in the body of a function are indented 2, 3, or 4 spaces. Statements inside of a loop or part of an if structure are indented further.
  • Use spaces around all operators. For example, write x = y + 5;, not x=y+5;
  • Using the emacs or xemacs editor along with the the .emacs file provided for you will automatically indent your code appropriately.

Use of Braces

  • Always use braces to mark the beginning and end of a loop or if structure, even when not required.
  • See the indentation standard for appropriate placement of braces.

Comments

Comments are the programmers main source of documentation. Comments for files, functions and code are described below.

File Header Comments

Every source file (.c and .h files) should contain an opening comment describing the contents of the file and other pertinent information. This file header comment must include the following information:

  • The file name
  • Your name
  • The date the file was created
  • Your section number
  • Your UMBC e-mail address
  • A description of what the program does

For example:

/*****************************************
** File:   proj1.c
** Author: Bob Smith
** Date:   9/1/16
** Section:01
** E-mail: bsmith22@gl.umbc.edu 
**
**   This file contains the main driver program for project 1.
** This program reads the file specified as the first command line
** argument, counts the number of words, spaces, and characters and
** displays the results in the format specified in the project description
**
** Other files required are
**      1. chars.c -- routines that count each type of character
**      2. words.c -- routines that count each word
**      3. proj1.h -- prototypes for all functions needed by this file
**
***********************************************/

Function Header Comments

Each function must have a header comment that includes the following:

  • function name
  • a description of what the function does
  • a description of the function's inputs
  • a description of the function's outputs
  • side effect(s) of the function (if any -- this should be rare)

For example:

/************************************************
** CircleArea --
**   This function calculates the area of a circle
** with the specified radius
**   Inputs: the circle's radius as a parameter
**   Output: the circle's area as the return value
**************************************************/

In-Line Comments

In-line comments are used to clarify what your code does, not how it does it. Well structured code will be broken into logical sections that perform a simple task. Each of these sections of code (typically starting with an if statement, or a loop or a switch) should be documented. A particularly important line of code should also be commented. Do not comment every line of code. Trivial comments (e.g. /** increment x **/) are worse than no comments at all. An in-line comment appears above the code to which it applies and is indented to the same level as the code. For example:

/* check every element of the array */
for (i = 0; i < ARRAYSIZE; i++)
{
    /* if it's odd, add one
    ** if even, do nothing */
    if (array[i] % 2 == 1)
    {
        array[i]++;
    }
}

Comments for #defines and Variables

#defines may be commented on the same line as which they occur. Such comments should be aligned with the comments for other #defines. Once again -- neatness counts! For example:

#define NRSTUDENTS 35    /* nr of students in the class */
#define NRSECTIONS  4    /* of CMSC104                  */
#define NRTAS       5    /* nr graduate student TAs     */

Variables may be commented in the same style as #defines if they are defined one per line as in the example below.

int total;       /* total students this section */
FILE *ifp;       /* input file pointer          */
double average;  /* class's final exam average  */

Summary of Emacs Commands

M-v Move backward one screenful

C-l Clear screen and redisplay everything (Putting the text near the cursor at the center of the screen)

C-p Move to the previous line

C-n Move to the next line

C-b Move backward one character

C-f Move forward one character

C-d Delete one character

C-k Kill from the cursor position to end of line

C-y To yank the lastly killed text back

C-a Move to the begining of the line

C-e Move to the end of the line

M-k Kill to the end of the current sentence

M-f Move forward one word

M-b Move backward one word

M-a Move to the begining of the sentence

M-e Move to the end of the sentence

M-< Move to the begining of the file

M-> Move to the end of the file

M-d Kill the next word after the cursor

C-x u Undoes one command's worth of changes

C-x C-s Save the file

C-x C-w Write to the file

C-x C-f retrieve/open a file

C-u n C-f Move forward n characters (n should be a number)

C-x C-c Save file and exit

Note: M stands for Meta-key and is usually the escape key

C Programming - Indentation Styles

Choose one of the two styles and use it consistently

if (condition)
{
   statement(s)
}

if (condition) {
   statement(s)
}
if (condition)
{
   statement(s)
}
else if (condition)
{
   statement(s)
}
else {
   statement(s)
}

if (condition) {
   statement(s)
} else if (condition) {
   statement(s)
} else {
   statement(s)
}
for (loop control expressions)
{
   statement(s)
}

for (loop control expressions) {
   statement(s)
}
while (condition)
{
   statement(s)
}

while (condition) {
   statement(s)
}
do
{
   statement(s)
}
while (condition);

do {
   statement(s)
} while (condition);
switch (integer expression)
{
   case constant1:
      statement(s)
   case constant2:
      statement(s)
   default:
      statement(s)
}

switch (integer expression) {
   case constant1:
      statement(s)
   case constant2:
      statement(s)
   default:
      statement(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment