Logo  

CS256 - Fundamentals of Structured Programming

Review:

The following are in brief everything that you should know or at least be aware of (i.e. know to look-up) thus far in this class. Please review the following carefully and look for more information in the lessons listed above or in the tutorialspoint site or elsewhere to make sure you fully understand each subject.

Unix commands

Command What is does
man Get help from the online manual
handin Assignment checkout and handin system
ls List files in a directory
cd Change your current working directory
cp Copy files and/or directories
mv Move/rename files and/or directories
rm Remove/delete files and/or directories
mkdir Make new empty directories
more View a file or output one screen at a time
chmod Change file permissions
nano A text-mode editor that is easier to pick up and learn

Quick Handin howto:

  • wiki link
  • Almost all handin commands should be performed from your home directory (~):

Checking for assignments and due-dates and submissions

handin

Checkout an assignment:

handin -C assignment-name

Use 'cd assignment-name' to change the working directory to the assignment directory and begin working.

Checking your work

handin --hwcheck assignment-name

Alternatively you can use check while inside of your assignment directory. Only works for assignments that can be auto-checked.

Submitting an assignment:

handin assignment-name

Alternatively you can use submit while inside of your assignment directory.

Checking the grading notes:

handin -g assignment-name


'ls -l' output decoded:

    -rw-r--r-- 1 user group   283 Jan 13 18:52 file
    │╲ ╱╲ ╱╲ ╱ │ ╲________╱  File ╲__________╱  └─── File-name
    │ U  G  O  │     │       Size      └──────────── Last modified time
    │  perms   │     └────────────────────────────── User and Group owners of the file
    │          └──────────────────────────────────── Hard link count (ignore it)
    └─────────────────────────────────────────────── File type

Also know:

  • / - (file path separator, by itself, the root of the filesystem)
  • . - (dot) By itself, the current working directory, a dot prefix makes files "hidden"
  • .. - (dot-dot) The parent directory of the current directory
  • ~ - Your home directory
  • ~username - Username's home directory.

Data-types:

Integer types:

C type: size: unsigned range: signed range: Examples:
char 8 bits 0-255 -128 - 127 'A', '\0'
int 32 bits 0-~4 billion +/- ~2 billion 1024, -65536
long 64 bits 0-~18 quintillion +/-9 quintillion 973945753

Floating point types:

C-type: size: range: Precision Examples:
float 32 bits ~ 10^+/-38 6 digits 1.5, -2.0
double 64 bits ~ 10^+/-308 15 digits .5, 5E16
  • Any number with a decimal point (i.e. may have a fractional value) or an exponent (engineering format) is a floating point number.

C character escapes and C strings:

Escape sequence
\' single quote
\" double quote
\\ backslash
\n line feed - new line (moves cursor to beginning of the next line)
\r carriage return (moves cursor back to the beginning of the line)
\t horizontal tab (moves cursor to next tab stop)
\nnn arbitrary octal value (nnn, where n is an octal digit (0-7))
\0 The null character (character at the end of all C strings.) Same as the arbitrary octal value above

C Grammars to remember

exp == an expression that evaluates to true or false (i.e. Boolean)
stmt == A statement

Grammar What it is Examples:
type var,var,...;
type var=exp;
Variable declaration int a, b, c;
int a = 52;
exp; An expression statement a++;
printf("foo");
var = exp; An assignment expression statement a=a+1;
a=foo();
{ stmt1; ...} Compound/block statement: a block of statements,
equivalent to just one statement
{
  foo();
  bar();
}
if (exp) stmt Do s if e is true if (a < 10) printf("a<10");
if (exp) stmt1
else stmt2
Do `stmt1` if `exp` is true,
otherwise do `stmt2`
if (a < 10) printf("a<10");
else printf("a>=10);
return-type name (variable-declarations)
{
  stmt1;
  ...
}
A function/procedure int add(int a, int b) {
  return a+b;
}
return exp; Gives a function its value. return 0;
return a+b;
// comment A single line comment. // Comment...
/* Comment */ A multi-line comment. /**
 * Comment
 */

C Expressions

Operators

Operator What it does: Example
Mathematical operators
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (remainder from integer division) a % b == integer remainder of a / b
++ Variable increment ++a (pre-increment), a++ (post-increment)
-- Variable decrement --a (pre-decrement), a-- (post-decrement)
Comparison operators
< Less than a < b
<= Less than or equal a <= b
> Greater than a > b (true if a is greater than b)
>= Greater than or equal a >= b (true if a is greater than or equal to b)
== Equal to a == b (true if a and b are the same)
!= Not equal to a != b (true if a and b are not the same)
Boolean Operators
&& Boolean AND a && b (true if both a & b are true)
|| Boolean OR a || b (true if either a or b are true)
! Boolean NOT ! a (true if a is false, false if a is true)
Assignment
= Assign right side to variable on left a = b + 1;

Boolean logic table:

a b NOT a a AND b a OR b
0 0 1 0 0
0 1 1 0 1
1 0 0 0 1
1 1 0 1 1

where 1 = true, and 0 = false.

Input/output: printf and scanf

Printf Format codes
Integers
%d Signed decimal (integer)
%ld Long integer
Floating point
%f Float
%lf Double (normal format)
Characters / strings
%c single character
%s String (null ('\0') terminated sequence of characters)
Misc
%% Output a literal '%' character
Scanf Format codes
Integers
%d Signed decimal (integer)
%ld Long integer
Floating point
%f Float (any format)
%lf Double (any format)
Characters / strings
%c A single character

Important:

  • When using scanf, integer and floating point variables require an ampersand (&) prefix (e.g. &var) which informs the scanf function about the location of the variable in memory so that the value read by scanf can be placed in the variable.

  • The return value for scanf should be equal to the number of things requested in the format string (i.e. the number of % format codes).