Lecture 15: Introduction to C
Overview
Burton Ma closes the Bash unit by explaining why arrays do not pass to functions like ordinary values and how Bash 4.3+ namerefs can pass them by name. He then introduces C: its history and performance-focused design, common uses, source and header files, compilation with GCC, and the roles of preprocessing, `main`, and exit status.
Key takeaways
- In Bash 4.3 and later, `local -n` creates a nameref that lets a function operate on an array passed by its variable name; ordinary `$ARR` expansion instead passes only the first element.
- C's efficiency-focused design reflects its origins on severely constrained hardware: the DEC PDP-7 had about 8 KB of memory, so C exposes more implementation details than Python or Java.
- A C source file is plain text, not an executable; compiling it with GCC creates a runnable program, named `a.out` by default or by a name specified with `-o`.
- C headers such as `stdio.h` provide declarations that tell the compiler which functions a program can call; `#include` is handled by the preprocessor before compilation.
- A C program in the course must have exactly one `main` function, whose integer return value becomes the Unix-like process exit status.
- C remains useful beyond standalone applications: Linux, embedded devices, graphics software, and implementations of languages such as Python reuse C or its libraries.
Chapters
- Passing `$ARR` supplies only the array's first element, while `${ARR[@]}` expands to separate arguments rather than passing an array object.
- A Bash 4.3+ function can use `local -n` to create a nameref and receive the array's name without a `$` prefix.
- Because Bash uses dynamic scoping, a function can also access an array defined in its caller's scope; Burton Ma recommends direct access when passing the array is unnecessary.
- The in-class Bash test is scheduled for the following Tuesday and allows no aids or cheat sheet.
- Students should know commands used in assignments and commonly used options, but do not need to memorize every command option.
- The course site and OnQ contain prior tests, exams, and sample questions; the most recent Bash midterm is recommended, while older exams include C questions to skip.
- `wc` reports counts such as lines, characters, and bytes.
- C is a general-purpose, compiled, procedural language, unlike purpose-focused languages such as Fortran for scientific computing and COBOL for business applications.
- C shares familiar C-family features with Java: braces, conditionals, loops, recursion, and static typing, where variable types are known at compile time.
- C is considered a low-level high-level language because its operations map relatively closely to computer hardware.
- Ken Thompson began developing the B language around 1968; Dennis Ritchie joined him in 1972, and they developed C while creating Unix.
- The DEC PDP-7 used for early development had about 8 KB of memory, illustrating why C emphasizes efficiency and gives programmers explicit control.
- C provides fewer conveniences than Python or Java; for example, its basic language offers limited string support compared with their managed strings.
- C's standards have changed relatively slowly, partly to preserve compatibility with software written as far back as the 1970s.
- Linux is written mostly in C, and C is common in embedded systems such as routers, where memory and processing power may be limited.
- The Python implementation and Java virtual machines or compilers are examples of software commonly implemented in C or C++.
- C and C++ are widely used for computer graphics, video games, and computationally intensive software; Fortran is another option for intensive scientific computing.
- Many modern languages can call C functions, allowing them to reuse C libraries and established software.
- Visual Studio Code with C extensions is Burton Ma's recommended editor; Eclipse with C plugins is another option, especially for its debugger.
- C programs use plain-text `.c` source files and `.h` header files, and a typical program combines multiple source files, headers, and libraries.
- A C program has one `main` function, unlike Java, where multiple classes can each define a main method.
- `#include <stdio.h>` provides declarations for standard I/O functions such as `puts`; angle brackets identify standard headers, while quotes are used for user-written headers.
- `puts` prints a string followed by a newline, while `printf` uses format specifiers such as `%s` to print formatted output.
- C source text is not directly executable by Linux; it must first be compiled, using GCC in the course.
- Compiling one source file without an output-name option creates an executable named `a.out`; the `-o` option names the output, as in `-o hello`.
- Editing a `.c` file does not update the existing executable; students must save changes and recompile before testing again.
- On macOS, a command called GCC may invoke Clang or a combination of compiler tools rather than the GNU compiler.
- Lines beginning with `#` are preprocessor directives; `#include` inserts a header's contents, typically function declarations, before compilation.
- Programs in the course need exactly one definition of `main`; temporary test mains in separate `.c` files must be removed or commented out when combining the program.
- The two standard forms shown are `int main(void)` for programs without command-line arguments and `int main(int argc, char *argv[])` for programs that accept them.
- `main` returns an integer used as the Unix-like process exit status: zero indicates success, and returning `10` produces exit status 10.
- The demonstration shows that omitting `return` from `main` still results in status zero in the environment used, though explicitly returning zero makes the intent clear.
Transcript excerpt
0:00 There's one last thing I want to talk about in Bash before we move on to the next major part of the course. And so we were talking about the last topic we talked about in Bash was writing functions. So there's a peculiar peculiarity in Bash in that you cannot pass an array to a function. Sorry, well you can but we haven't I haven't shown you how to do it. So here's a little Bash script that tries to pass an array to a function. So here's my function use array. Now remember if you're going to if a if a function has some arguments the arguments are stored in the special parameters 1 2 3 4 and so on. So I'm going to try to pass an array to
0:47 this function and I'm going to pass I'm going to try to access that array using special parameter number one. And all I'm going to do is I'm going to print the length of the array. So remember that's how you get the length of an array. And I'm going to pass a non well I'm going to pass a what I think is a non-empty array to the function and I'm going to try to print the first element. So there's my array. It's just the array 1 2 3. And now this might be your first hint that there's a problem passing arrays to functions. And so remember if you want to pass a value to a function normally you would have to write dollar you need to you need to write dollar in front of the parameter in certain front of the variable name sorry. Because you want to pass the value of that thing to a function.
1:35 And so the question is is what the heck
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Burton Ma.