Brian Lee's College Portfolio
Learnings
Here are some notes that I took while studying different subjects.
Practical Programming in C
Source: MITOpenCourseWare
C Language Overview
Created by Dennis Ritchie at Bell Labs in 1972
Low-level, efficient language
Few keywords, has structures, unions, pointers, libraries
Writing C Programs
Use text editor (.c files)
Compile with gcc to .o object file
Use gdb debugger or IDE like Eclipse
Program Structure
Comments
Include headers
Variable declarations
main() entry function
Function prototypes and definitions
Expressions, arithmetic operations
puts() to print to console
#define for constants/macros
Compile with gcc, run program
Variables and Data Types
Variables store values in memory, must be declared with type
Variable naming rules - start with letter, can have letters/digits
Data types: int, float, double, char for numeric/character data
Numeric types have signed/unsigned versions affecting range
Constants can be integers, floats, chars specified in various ways
Declarations specify type and optionally initialize value
Operators
Arithmetic: +, -, *, /, %, increment/decrement
Relational: >, >=, Logical: &&, ||, !
Bitwise: &, |, ^, >
Assignment: +=, -=, *=, /=, %=, ?:
Operator precedence and order of evaluation rules
Use () to clarify precedence
Type Conversions
Automatic conversions between types in expressions
Lower types promoted to higher types
char converted to int
Assign higher precision type to lower loses data
Control Flow
Conditional statements: if, else if, else
Switch statement for multiple cases
Loops: while, for, do-while
Break to exit loop, continue to skip iteration
Functions
Divide program into sub-problems/functions
Functions have parameters, return value
Call by value parameter passing
Recursion vs iteration
Pointers and Memory
Pointers store memory addresses of variables
Physical vs virtual memory
Addressing variables with & operator
Dereferencing pointers with * operator
Pointer arithmetic
Functions with Multiple Outputs
Use pointers to return multiple values
Pass pointers as arguments to modify variables
Arrays and Pointer Arithmetic
Arrays implemented as pointers to contiguous memory
sizeof() operator for data sizes
Pointer arithmetic to access array elements
String utility functions in string.h
Searching and Sorting Algorithms
Linear search - iterate through array
Insertion sort - shift out-of-order elements
Quicksort - divide and conquer, recursive
Binary search - logarithmic for sorted arrays
Some examples:
Swapping two integers with pointers
Iterative implementation of insertion sort
Recursive implementation of quicksort
Input/Output
I/O facilities provided by stdio.h library
Text streams vs binary streams
Buffered I/O
Standard I/O
getchar(), putchar() for single character input/output
printf() for formatted output
Format specifiers for integers, floats, strings etc.
Flags, width, precision modifiers
String I/O
Strings represented as null-terminated char arrays
strcmp() for string comparison
sprintf(), sscanf() for formatted string I/O
File I/O
FILE pointers from fopen()
fclose() to close files
getc(), putc() for single char file I/O
fgets(), fputs() for line I/O
fscanf() for formatted file input
Command Line Arguments
argc stores argument count
argv[] stores argument strings
argv[0] is program name
Examples:
Making uppercase to lowercase conversion
Taking input line and parsing integers
Reading from file and writing to another file
Passing multiple arguments from command line