Programming Concepts Study Guide

Unix Operating System • Programming Paradigms • Language Structure • Development Environment

Learning Objectives

Unix Operating System

Unix: A family of multitasking, multiuser computer operating systems derived from AT&T Unix. Known for its philosophy of "do one thing and do it well."

Core Unix Principles

Essential Unix Commands

# File operations ls -la # List files with details cp source destination # Copy files mv old_name new_name # Move/rename files rm filename # Remove files mkdir dirname # Create directory # Text processing cat filename # Display file contents grep "pattern" filename # Search for patterns head -n 10 filename # First 10 lines tail -f logfile # Monitor file changes # Process management ps aux # List running processes kill -9 PID # Terminate process jobs # Show background jobs nohup command & # Run command in background # File permissions chmod 755 filename # Set permissions (rwxr-xr-x) chown user:group file # Change ownership

File System Structure

/ # Root directory ├── bin/ # Essential command binaries ├── etc/ # System configuration files ├── home/ # User home directories ├── usr/ # User utilities and applications │ ├── bin/ # User command binaries │ └── lib/ # Libraries ├── var/ # Variable files (logs, mail, etc.) └── tmp/ # Temporary files

Programming Paradigms

Programming Paradigm: A fundamental style or approach to programming that provides a conceptual framework for structuring and organizing code.

Imperative Programming

Concept: Programs consist of a sequence of statements that change program state.

Focus: HOW to solve the problem (step-by-step instructions)

// C - Imperative example: Calculate factorial #include <stdio.h> int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result = result * i; } return result; } int main() { int num = 5; int result = factorial(num); printf("Factorial of %d is %d\n", num, result); return 0; }

Functional Programming

Concept: Computation as evaluation of mathematical functions, avoiding state and mutable data.

Focus: WHAT the problem is (mathematical relationships)

-- Haskell - Functional example: Calculate factorial factorial :: Integer -> Integer factorial 0 = 1 factorial n = n * factorial (n - 1) -- List processing with higher-order functions squares = map (^2) [1, 2, 3, 4, 5] evens = filter even [1, 2, 3, 4, 5, 6] sum_list = foldl (+) 0 [1, 2, 3, 4, 5] main = do print (factorial 5) print squares print evens print sum_list

Logic Programming

Concept: Programs are sets of logical statements and rules. The system finds solutions through logical inference.

Focus: WHAT is true (declarative facts and relationships)

% Prolog - Logic programming example: Family relationships parent(tom, bob). parent(tom, liz). parent(bob, ann). parent(bob, pat). parent(pat, jim). % Rules grandparent(X, Z) :- parent(X, Y), parent(Y, Z). sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y. % Queries % ?- grandparent(tom, ann). % Is tom a grandparent of ann? % ?- sibling(ann, pat). % Are ann and pat siblings? % ?- grandparent(tom, X). % Who are tom's grandchildren?

Paradigm Comparison

Paradigm Control Flow State Management Problem Solving
Imperative Explicit (loops, conditions) Mutable state Step-by-step instructions
Functional Function calls/recursion Immutable state Function composition
Logic Inference engine Fact/rule database Logical deduction

Structure of Programming Languages

Language Components

Syntax

The rules that define valid constructs in a programming language (grammar and structure).

Semantics

The meaning of syntactically correct constructs (what the code actually does).

Pragmatics

How the language is used in practice (idioms, best practices, real-world usage).

Language Design Elements

/* Data Types */ int number = 42; // Integer float pi = 3.14159; // Floating point char letter = 'A'; // Character char* string = "Hello"; // String (array of chars) bool flag = true; // Boolean /* Control Structures */ // Selection if (condition) { // execute if true } else { // execute if false } switch (value) { case 1: /* code */ break; case 2: /* code */ break; default: /* code */ break; } // Iteration for (int i = 0; i < 10; i++) { // loop body } while (condition) { // loop body } do { // loop body } while (condition);

Memory Management Models

Model Description Examples Pros/Cons
Manual Programmer controls allocation/deallocation C, C++ Fast, predictable / Error-prone
Garbage Collection Automatic memory management Java, Python, JavaScript Safe, convenient / Runtime overhead
Reference Counting Track references to objects Swift, Python (partial) Deterministic / Circular references

Macros vs Procedures

Macro: A rule or pattern that specifies how input text should be mapped to replacement text. Processed at compile-time through textual substitution.
Procedure: A named sequence of instructions that performs a specific task. Executed at runtime through function calls.

Macro Example

// C Preprocessor macros #define PI 3.14159 #define SQUARE(x) ((x) * (x)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) // Usage double area = PI * SQUARE(radius); // Expanded at compile time int maximum = MAX(10, 20); // Expanded at compile time // Macro expansion (what the preprocessor generates): double area = 3.14159 * ((radius) * (radius)); int maximum = ((10) > (20) ? (10) : (20));

Procedure Example

// C functions (procedures) const double PI = 3.14159; double square(double x) { return x * x; } double max(double a, double b) { return (a > b) ? a : b; } // Usage double area = PI * square(radius); // Function call at runtime double maximum = max(10.0, 20.0); // Function call at runtime

Key Differences

Aspect Macros Procedures
Processing Time Compile-time (preprocessing) Runtime
Type Checking No type checking Full type checking
Memory Usage Code expansion (inline) Single copy in memory
Debugging Difficult to debug Easy to debug
Speed No function call overhead Function call overhead
Side Effects Can have unexpected side effects Predictable behavior

Macro Pitfall Example

#define SQUARE(x) x * x int result = SQUARE(3 + 4); // Expands to: 3 + 4 * 3 + 4 = 19 (not 49!) // Correct macro definition: #define SQUARE(x) ((x) * (x))

History of Programming Languages

1940s
Machine Language & Assembly

Binary machine code and assembly language mnemonics. Direct hardware control.

1957
FORTRAN

First high-level programming language. Designed for scientific computing by IBM.

1958
LISP

List Processing language. Introduced functional programming concepts.

1959
COBOL

Common Business-Oriented Language. Designed for business applications.

1964
BASIC

Beginner's All-purpose Symbolic Instruction Code. Easy-to-learn language for education.

1970
Pascal

Designed for teaching structured programming. Strong type system.

1972
C

System programming language. Influenced most modern languages.

1972
Prolog

Logic programming language based on formal logic.

1980s
C++

Object-oriented extension of C. Introduced classes and inheritance.

1990s
Java, Python, JavaScript

Platform independence, scripting, and web development revolution.

Language Evolution Trends

GNU GCC Programming Environment

GCC (GNU Compiler Collection): A comprehensive set of compilers for various programming languages including C, C++, Fortran, and others.

Compilation Process

# Basic compilation gcc hello.c -o hello # Compile C program gcc -g hello.c -o hello # Compile with debugging info gcc -O2 hello.c -o hello # Compile with optimization # Multi-file compilation gcc main.c utils.c -o program gcc -c utils.c # Compile to object file gcc main.c utils.o -o program # Link with object file # Common flags gcc -Wall -Wextra hello.c # Enable warnings gcc -std=c99 hello.c # Specify C standard gcc -I/path/to/headers hello.c # Include directory gcc -L/path/to/libs -lmath hello.c # Link libraries

Compilation Stages

# 1. Preprocessing (expand macros, include files) gcc -E hello.c -o hello.i # 2. Compilation (C to assembly) gcc -S hello.c -o hello.s # 3. Assembly (assembly to machine code) gcc -c hello.c -o hello.o # 4. Linking (combine object files) gcc hello.o -o hello # All stages in one command gcc hello.c -o hello

Makefile Example

# Simple Makefile CC = gcc CFLAGS = -Wall -Wextra -std=c99 TARGET = program SOURCES = main.c utils.c math_ops.c # Default target all: $(TARGET) # Build target $(TARGET): $(SOURCES) $(CC) $(CFLAGS) $(SOURCES) -o $(TARGET) # Clean up clean: rm -f $(TARGET) *.o # Debug build debug: CFLAGS += -g -DDEBUG debug: $(TARGET) # Install target install: $(TARGET) cp $(TARGET) /usr/local/bin/ .PHONY: all clean debug install

Debugging with GDB

# Compile with debug information gcc -g -o program program.c # Start GDB gdb ./program # GDB commands (gdb) break main # Set breakpoint at main (gdb) run # Start program execution (gdb) next # Execute next line (gdb) step # Step into function calls (gdb) print variable # Print variable value (gdb) list # Show source code (gdb) backtrace # Show call stack (gdb) continue # Continue execution (gdb) quit # Exit GDB

Glossary of Terms

Assembly Language: A low-level programming language with a strong correspondence between language instructions and machine code instructions.
Compiler: A program that translates source code written in a high-level language to machine code.
Control Structure: Programming constructs that control the flow of execution (if-else, loops, switch statements).
Function/Procedure: A named block of code that performs a specific task and can be called from other parts of the program.
Higher-Order Function: A function that takes other functions as arguments or returns functions as results.
Immutable: Data that cannot be changed after it is created. Common in functional programming.
Interpreter: A program that executes source code directly without prior compilation to machine code.
Linking: The process of combining multiple object files and libraries into a single executable program.
Object File: A file containing compiled machine code that hasn't yet been linked into an executable.
Recursion: A programming technique where a function calls itself to solve smaller instances of the same problem.
Shell: A command-line interface that provides access to Unix operating system services.
Static Typing: A type system where variable types are determined at compile time.
Syntax: The set of rules that defines valid constructs in a programming language.
Variable: A named storage location in memory that holds a value that can change during program execution.