Lecture File
c_pl.pdf (The C Programming Language)
C Core
C syntax, number systems, memory representation, and function mechanics.
File/lecture name: c_pl.pdf (The C Programming Language) Main theme: Introduction to C syntax, number systems, memory layout, and function mechanics. Prereqs assumed by slides: Basic computer literacy, familiarity with Python (for comparison), understanding of binary logic (1s and 0s). What this lecture enables you to do:
gcc.printf formatting.main).main, #include, printf, and return values.char type, ASCII encoding, and string literals vs. character literals.printf format specifiers (%d, %f, %c, %s).if, while, and for loops (syntax differences from Python).{}.gcd function demonstrating stack growth.| Name | Definition | Why it matters | Common confusion / misconception | Tiny example |
|---|---|---|---|---|
| Compiled Language | A language where a compiler (e.g., gcc) generates machine code (binary) from source code. |
Allows direct execution by CPU; faster than interpreted languages. | Confusing gcc with an interpreter; gcc invokes preprocessor, compiler, assembler, linker. |
gcc hello.c ./a.out |
| Statically Typed | Types of variables must be specified at declaration and cannot change. | Catches type errors at compile time; memory layout is known. | Thinking C types are like Python classes; C types are primitive (int, char). | int x = 5; (cannot change to string). |
| Entry Point | The specific function where execution begins (always main in C). |
OS loads program and jumps here; defines program exit status. | Thinking main is optional; unlike Python, C requires main. |
int main(int argc, char **argv) |
| Two's Complement | Method for storing signed integers: flip bits and add 1 to negative numbers. | Standard for signed integers; handles addition/subtraction uniformly. | Confusing with Ones' Complement (which has two zeros). | 1111 1111 is -1 in 8-bit. |
| Stack Frame | A portion of the stack allocated for a function call (parameters + locals). | Explains variable lifetime and isolation between function calls. | Thinking parameters modify the original argument; they are copies. | times2(x) modifies local x, not caller's x. |
| Memory Segments | Regions of RAM: Text (code), Data (globals), Heap (dynamic), Stack (locals). | Determines where variables live and how long they persist. | Assuming all variables live in the same place; main args are on stack. |
static vars go in Data; malloc goes in Heap. |
printf Specifiers |
Placeholders in format string (%d, %f, %c, %s) for arguments. |
Allows printing different types in a single call. | Mismatching specifier and argument type (e.g., %d with float). |
printf("%d", 3.14) prints garbage. |
| Scope | The region of code where a variable is accessible (defined by {}). |
Prevents name collisions; variables die when scope ends. | Thinking variables persist after function returns; they don't. | int y inside {} is inaccessible outside. |
How to Compile and Run a C Program
hello.c).gcc filename.c.a.out.-o flag (gcc filename.c -o myprog)../binary_name.-o output.How to Reason About Signed Integers
int (signed) or unsigned int.How to Use printf Safely
"x: %d\n").%d for int, %f for float).%.2f to limit decimal places for floats.How to Manage Loop Variables
for loop syntax: for (init; cond; update) { ... }.init part (e.g., int i = 0).cond evaluates to non-zero (true) to continue.update modifies the variable to prevent infinite loops.How to Understand Function Calls
main).| Pattern Name | When to use it | Correct minimal example | Common bug + how to avoid it |
|---|---|---|---|
Standard main |
Entry point for every C program. | int main(int argc, char **argv) { ... return 0; } |
Forgetting return 0; (exit status undefined). |
printf Formatting |
Printing mixed types. | printf("%d %c %f\n", x, c, z); |
Using %s for a char (prints address) or %d for float. |
for Loop |
Iterating with known count or state change. | for (int i=0; i<10; ++i) { ... } |
Declaring i outside loop if not needed; forgetting ++i. |
| Function Call | Reusing logic. | int result = gcd(a, b); |
Passing wrong number of arguments; mismatched types. |
| Character Literal | Storing a single character. | char c = 'A'; |
Using double quotes "A" (creates a string, not a char). |
printf format specifier mismatch (e.g., %d with float).printf arguments against format string.int x = 0;).main function returns non-zero exit code.return statement in main returns value other than 0.return 0 for success (standard convention).printf prints nothing or wrong number.\n or wrong specifier.\n or correct %d/%f.main".main function or typo in signature.int main(...) exists.unsigned with negative logic.signed int for negative values.y not accessible outside function.{} (local scope).times2 function doesn't change caller's variable.printf prints address of char instead of char.%s for char or %d for char.%c for char.return in main returns garbage.return statement.return 0;.for loop infinite loop.update statement missing or condition never false.update modifies loop variable.gcd recursion causes stack overflow.b decreases in recursive call.char arithmetic results in unexpected values.'A' is a number (65), not a string.return value ignored by caller.int x = gcd(a, b);.main returns 0?
A: The program exits successfully (standard convention).int main(int argc, char **argv) imply about command line arguments?
A: argc is the count of arguments; argv is an array of pointers to strings containing the arguments.'A' and "A"?
A: 'A' is a single character (type char), "A" is a string literal (type char[]).int x = 5; and int y = 10;, what is the result of x = x + y;?
A: x becomes 15.times2(x) not change x in main?
A: Because C uses pass-by-value; the parameter x in the function is a copy.-1 in an 8-bit Two's Complement system?
A: 1111 1111 (flip 0000 0001 to 1111 1110, add 1).%d to print a float?
A: Undefined behavior (likely prints garbage or crashes).main function.#include <stdio.h> directive?
A: It includes the standard I/O library, making functions like printf available.100 + 1 in binary if the result overflows an 8-bit container?
A: The carry bit is lost (overflow), resulting in 100 (4) in binary (modulo 256).return 0 in main signify to the Operating System?
A: It signifies successful completion of the program.gcc Compilation:gcc file.c outputs a.outgcc file.c -o name outputs nameprintf Specifiers:%d int%f float%c char%s string (array of chars)main Signature:int main(int argc, char **argv)argc: count of argumentsargv: array of argument strings0 falsetrue&& AND|| OR! NOTfor Loop:for (init; cond; update) { ... }return in main:return 0 Successreturn 1 Error (convention)main).argc: Argument count (integer).argv: Argument vector (array of strings).printf: Standard output function with formatting.#include: Preprocessor directive to include libraries.return: Exits a function and optionally returns a value.char: Type representing a single byte/character.int: Type representing an integer.float: Type representing a floating-point number.unsigned: Type modifier indicating non-negative integers.static: Type modifier indicating data stored in the Data segment.heap: Memory segment for dynamic allocation.text: Memory segment for executable code.data: Memory segment for global/static variables.char **argv is shown, pointer arithmetic and dereferencing are not explicitly taught in these slides.heap segment is mentioned, but malloc, calloc, and free are not covered.fopen, fread) is not covered.gcd example).#include is shown; #define, #ifdef are not covered.errno, strerror, or signal handling are not covered.#include <stdio.h>, int main(int argc, char **argv), printf.Manually curated from summaries/c_pl.txt. Use this page as a study aid and cross-check official slides for grading-critical details.