Show description
CSE 240 - C/C++ Cheat Sheet
CSE 240 - C/C++ Cheat Sheet
CSE 240: C & C++ Mastery Guide
Module 1: Paradigms, Errors, Processing & Macros
Programming Paradigms
Imperative (Non-Declarative): Focuses on how to execute.
Procedural: C, FORTRAN. Based on functions/procedures.
Object-Oriented (OOP): C++, Java. Encapsulates data and behavior.
Declarative: Focuses on what to execute.
Functional: LISP, Scheme. Avoids state changes and side effects.
Logic: Prolog. Uses facts and rules.
Program Error Types
Contextual Error: A "type error" found by the compiler. E.g., `int x = "hello";`
Semantic Error: A logic error found at runtime. E.g., division by zero.
Macros in C/C++
Macros perform text substitution during pre-processing. They avoid function call overhead but are prone to side effects.
// Define a macro to find the maximum value.
// Note the parentheses around the arguments and the whole expression to avoid operator precedence issues.
#define MAX(x, y) ((x) > (y) ? (x) : (y))
// --- SIDE EFFECT EXAMPLE ---
int a = 5;
int z = MAX(a++, 10);
// The preprocessor expands this to:
int z = ((a++) > (10) ? (a++) : (10));
// If a++ > 10 is true, 'a' would be incremented twice! This is unexpected behavior.
Module 2: Data Types & Basic I/O
Imperative Data & State
Data is represented by state: a collection of variables and their values. Key concepts:
Address: An integer representing a memory location.
Pointer: A variable that stores an address.
Scope: The region of code where a variable is accessible.
Basic I/O
C-Style: `scanf` and `printf`
#include <stdio.h>
int num;
printf("Enter a number: ");
// scanf needs the ADDRESS (&) of a variable to store the input.
scanf("%d", &num); // %d is the format specifier for an integer.
printf("You entered: %d\n", num);
C++ Style: `cin` and `cout`
#include <iostream>
int num;
std::cout > num;
std::cout
CSE 240 - C/C++ Cheat Sheet
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSE 240 - C/C++ Cheat Sheet</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* Custom dark theme styles */
body {
background-color: #1a1a1a;
color: #d1d5db; /* gray-300 */
}
h1, h2, h3 {
color: #93c5fd; /* blue-300 */
font-family: 'Inter', sans-serif;
}
h2 {
border-bottom-color: #4b5563; /* gray-600 */
}
.container {
background-color: #2a2a2e;
border-color: #4b5563; /* gray-600 */
}
hr {
border-color: #4b5563; /* gray-600 */
}
code {
background-color: #111827; /* gray-900 */
color: #f9fafb; /* gray-50 */
border-radius: 0.25rem;
padding: 0.125rem 0.25rem;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
}
pre {
background-color: #0d1117;
border: 1px solid #30363d;
border-radius: 6px;
color: #c9d1d9;
}
pre .comment { color: #8b949e; }
pre .keyword { color: #ff7b72; }
pre .type { color: #a5d6ff; }
pre .string { color: #79c0ff; }
pre .number { color: #f78166; }
pre .operator { color: #ff7b72; }
strong { color: #fca5a5; } /* red-300 */
</style>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&display=swap" rel="stylesheet">
</head>
<body class="font-sans">
<div class="max-w-4xl mx-auto p-4 sm:p-6 lg:p-8">
<h1 class="text-4xl font-bold text-center mb-8">CSE 240: C & C++ Mastery Guide</h1>
<!-- MODULE 1 -->
<div class="container my-6 p-6 rounded-lg border shadow-md">
<h2 class="text-3xl font-semibold mb-4 pb-2 border-b">Module 1: Paradigms, Errors, Processing & Macros</h2>
<h3 class="text-2xl font-medium mt-4">Programming Paradigms</h3>
<ul class="list-disc list-inside mt-2 space-y-2">
<li><strong>Imperative (Non-Declarative):</strong> Focuses on <strong>how</strong> to execute.
<ul class="list-circle list-inside ml-6">
<li><strong>Procedural:</strong> C, FORTRAN. Based on functions/procedures.</li>
<li><strong>Object-Oriented (OOP):</strong> C++, Java. Encapsulates data and behavior.</li>
</ul>
</li>
<li><strong>Declarative:</strong> Focuses on <strong>what</strong> to execute.
<ul class="list-circle list-inside ml-6">
<li><strong>Functional:</strong> LISP, Scheme. Avoids state changes and side effects.</li>
<li><strong>Logic:</strong> Prolog. Uses facts and rules.</li>
</ul>
</li>
</ul>
<hr class="my-6">
<h3 class="text-2xl font-medium mt-4">Program Error Types</h3>
<ul class="list-disc list-inside mt-2">
<li><strong>Contextual Error:</strong> A "type error" found by the compiler. E.g., `int x = "hello";`</li>
<li><strong>Semantic Error:</strong> A logic error found at runtime. E.g., division by zero.</li>
</ul>
<hr class="my-6">
<h3 class="text-2xl font-medium mt-4">Macros in C/C++</h3>
<p class="mt-2">Macros perform text substitution during <strong>pre-processing</strong>. They avoid function call overhead but are prone to side effects.</p>
<pre class="p-4 rounded-md mt-2 whitespace-pre-wrap"><span class="comment">// Define a macro to find the maximum value.
// Note the parentheses around the arguments and the whole expression to avoid operator precedence issues.</span>
<span class="keyword">#define</span> <span class="type">MAX</span>(x, y) ((x) > (y) ? (x) : (y))
<span class="comment">// --- SIDE EFFECT EXAMPLE ---</span>
<span class="type">int</span> a = <span class="number">5</span>;
<span class="type">int</span> z = <span class="type">MAX</span>(a++, <span class="number">10</span>);
<span class="comment">// The preprocessor expands this to:</span>
<span class="type">int</span> z = ((a++) > (<span class="number">10</span>) ? (a++) : (<span class="number">10</span>));
<span class="comment">// If a++ > 10 is true, 'a' would be incremented twice! This is unexpected behavior.</span>
</pre>
</div>
<!-- MODULE 2 -->
<div class="container my-6 p-6 rounded-lg border shadow-md">
<h2 class="text-3xl font-semibold mb-4 pb-2 border-b">Module 2: Data Types & Basic I/O</h2>
<h3 class="text-2xl font-medium mt-4">Imperative Data & State</h3>
<p class="mt-2">Data is represented by <strong>state</strong>: a collection of variables and their values. Key concepts:</p>
<ul class="list-disc list-inside mt-2">
<li><strong>Address:</strong> An integer representing a memory location.</li>
<li><strong>Pointer:</strong> A variable that stores an address.</li>
<li><strong>Scope:</strong> The region of code where a variable is accessible.</li>
</ul>
<hr class="my-6">
<h3 class="text-2xl font-medium mt-4">Basic I/O</h3>
<h4 class="text-xl font-medium mt-4">C-Style: `scanf` and `printf`</h4>
<pre class="p-4 rounded-md mt-2 whitespace-pre-wrap"><span class="keyword">#include</span> <span class="string"><stdio.h></span>
<span class="type">int</span> num;
printf(<span class="string">"Enter a number: "</span>);
<span class="comment">// scanf needs the ADDRESS (&) of a variable to store the input.</span>
scanf(<span class="string">"%d"</span>, <span class="operator">&</span>num); <span class="comment">// %d is the format specifier for an integer.</span>
printf(<span class="string">"You entered: %d\n"</span>, num);
</pre>
<h4 class="text-xl font-medium mt-4">C++ Style: `cin` and `cout`</h4>
<pre class="p-4 rounded-md mt-2 whitespace-pre-wrap"><span class="keyword">#include</span> <span class="string"><iostream></span>
<span class="type">int</span> num;
std::cout << <span class="string">"Enter a number: "</span>;
std::cin >> num;
std::cout << <span class="string">"You entered: "</span> << num << std::endl;
</pre>
</div>
<!-- MODULE 3 -->
<div class="container my-6 p-6 rounded-lg border shadow-md">
<h2 class="text-3xl font-semibold mb-4 pb-2 border-b">Module 3: Pointers, Arrays, Structs & New Types</h2>
<h3 class="text-2xl font-medium mt-4">Pointers</h3>
<p class="mt-2">A variable that stores the memory address of another variable.</p>
<ul class="list-disc list-inside mt-2">
<li><code>&</code> <strong>(Address-of Operator):</strong> Gets the memory address of a variable.</li>
<li><code>*</code> <strong>(Dereference Operator):</strong> Gets the value at the address a pointer is holding.</li>
</ul>
<pre class="p-4 rounded-md mt-2 whitespace-pre-wrap"><span class="type">int</span> score = <span class="number">100</span>;
<span class="type">int</span>* scorePtr = <span class="operator">&</span>score; <span class="comment">// Pointer holds the address of score.</span>
<span class="comment">// To change score's value via the pointer, you must dereference it.</span>
<span class="operator">*</span>scorePtr = <span class="number">150</span>;
printf(<span class="string">"New score: %d\n"</span>, score); <span class="comment">// Prints "New score: 150"</span>
</pre>
<hr class="my-6">
<h3 class="text-2xl font-medium mt-4">Structs</h3>
<p class="mt-2">A composite data type that groups variables. Access members with the dot operator (<code>.</code>).</p>
<pre class="p-4 rounded-md mt-2 whitespace-pre-wrap"><span class="keyword">struct</span> <span class="type">Player</span> {
<span class="type">char</span> name[<span class="number">50</span>];
<span class="type">int</span> health;
};
<span class="type">void</span> main() {
<span class="keyword">struct</span> <span class="type">Player</span> p1;
strcpy(p1.name, <span class="string">"Zelda"</span>);
p1.health = <span class="number">100</span>;
}
</pre>
</div>
<!-- MODULE 4 -->
<div class="container my-6 p-6 rounded-lg border shadow-md">
<h2 class="text-3xl font-semibold mb-4 pb-2 border-b">Module 4: Padding, Files, Parameters & Linked Lists</h2>
<h3 class="text-2xl font-medium mt-4">Structure Padding</h3>
<p class="mt-2">Compilers add invisible bytes within structs to align members to memory addresses that are multiples of the word size (e.g., 4 bytes). This improves CPU access speed.</p>
<hr class="my-6">
<h3 class="text-2xl font-medium mt-4">Parameter Passing</h3>
<ul class="list-disc list-inside mt-2">
<li><strong>Pass-by-Value:</strong> Function gets a <strong>copy</strong>. Original variable is unchanged.</li>
<li><strong>Pass-by-Address (Pointer):</strong> Function gets the <strong>address</strong>. Can modify the original variable via dereferencing.</li>
<li><strong>Pass-by-Alias (Reference - C++):</strong> Function gets an <strong>alias</strong>. Modifies the original variable directly.</li>
</ul>
<hr class="my-6">
<h3 class="text-2xl font-medium mt-4">Linked Lists</h3>
<p class="mt-2">A dynamic data structure where nodes are linked using pointers.</p>
<h4 class="text-xl font-medium mt-4">Node Structure & Traversal</h4>
<pre class="p-4 rounded-md mt-2 whitespace-pre-wrap"><span class="keyword">struct</span> <span class="type">Node</span> {
<span class="type">int</span> data;
<span class="keyword">struct</span> <span class="type">Node</span>* next; <span class="comment">// Self-referential pointer.</span>
};
<span class="comment">// To traverse without losing the list, use a temporary pointer.</span>
<span class="type">void</span> printList(<span class="keyword">struct</span> <span class="type">Node</span>* head) {
<span class="keyword">struct</span> <span class="type">Node</span>* temp = head;
<span class="keyword">while</span> (temp != <span class="keyword">NULL</span>) {
printf(<span class="string">"%d -> "</span>, temp->data);
temp = temp->next; <span class="comment">// Advance the temporary pointer.</span>
}
}
</pre>
</div>
<!-- MODULE 5 -->
<div class="container my-6 p-6 rounded-lg border shadow-md">
<h2 class="text-3xl font-semibold mb-4 pb-2 border-b">Module 5: Recursion & Recursive Data Structures</h2>
<h3 class="text-2xl font-medium mt-4">Recursion</h3>
<p>A function that calls itself. Every recursive solution must have:</p>
<ul class="list-disc list-inside mt-2">
<li>A <strong>base case</strong> (stopping condition) to prevent infinite loops.</li>
<li>A <strong>recursive step</strong> that moves the problem closer to the base case.</li>
</ul>
<pre class="p-4 rounded-md mt-2 whitespace-pre-wrap"><span class="type">int</span> factorial(<span class="type">int</span> n) {
<span class="comment">// Base Case: The simplest problem we can solve.</span>
<span class="keyword">if</span> (n <= <span class="number">1</span>) {
<span class="keyword">return</span> <span class="number">1</span>;
}
<span class="comment">// Recursive Step: Solve a smaller version of the same problem.</span>
<span class="keyword">else</span> {
<span class="keyword">return</span> n <span class="operator">*</span> factorial(n - <span class="number">1</span>);
}
}
</pre>
<hr class="my-6">
<h3 class="text-2xl font-medium mt-4">Binary Search Trees (BST)</h3>
<p class="mt-2">A node-based structure where for any node, all keys in the left subtree are smaller, and all keys in the right subtree are larger.</p>
<ul class="list-disc list-inside mt-2">
<li><strong>Search Complexity:</strong> <strong>$O(\log n)$</strong> for a <strong>balanced</strong> tree. Very efficient. Degrades to $O(n)$ if unbalanced.</li>
<li><strong>Traversals:</strong>
<ul class="list-circle list-inside ml-6">
<li><strong>Inorder (Left, Root, Right):</strong> Produces a <strong>sorted</strong> list of the data.</li>
<li><strong>Preorder (Root, Left, Right)</strong></li>
<li><strong>Postorder (Left, Right, Root)</strong></li>
</ul>
</li>
</ul>
</div>
<!-- MODULE 6 -->
<div class="container my-6 p-6 rounded-lg border shadow-md">
<h2 class="text-3xl font-semibold mb-4 pb-2 border-b">Module 6: OOP Basics & Memory Management</h2>
<h3 class="text-2xl font-medium mt-4">Memory Management in C/C++</h3>
<ul class="list-disc list-inside mt-2">
<li><strong>Stack:</strong> For local variables. Fast, automatic, limited size. Memory is freed when a function returns.</li>
<li><strong>Heap:</strong> For dynamic memory (<code>malloc</code>/<code>new</code>). Flexible, large, but requires manual management (<code>free</code>/<code>delete</code>).</li>
<li><strong>Memory Leak:</strong> Forgetting to <code>free</code>/<code>delete</code> heap memory.</li>
<li><strong>Dangling Pointer:</strong> A pointer to memory that has already been freed.</li>
</ul>
<hr class="my-6">
<h3 class="text-2xl font-medium mt-4">Classes, Constructors & Destructors (C++)</h3>
<p class="mt-2">A <strong>class</strong> is a blueprint for objects.</p>
<ul class="list-disc list-inside mt-2">
<li><strong>Constructor:</strong> Special function called when an object is created. Used for initialization.</li>
<li><strong>Destructor:</strong> Special function (<code>~ClassName</code>) called when an object is destroyed. Used for cleanup, especially freeing heap memory.</li>
</ul>
<pre class="p-4 rounded-md mt-2 whitespace-pre-wrap"><span class="keyword">class</span> <span class="type">Character</span> {
<span class="keyword">public:</span>
<span class="comment">// Constructor: allocates memory on the heap for the name.</span>
<span class="type">Character</span>(<span class="keyword">const</span> <span class="type">char</span>* newName) {
name = new <span class="type">char</span>[strlen(newName) + <span class="number">1</span>];
strcpy(name, newName);
}
<span class="comment">// Destructor: frees the heap memory to prevent a leak.</span>
~<span class="type">Character</span>() {
<span class="keyword">delete</span>[] name;
}
<span class="keyword">private:</span>
<span class="type">char</span>* name;
};
</pre>
</div>
</div>
</body>
</html>