Lecture 15: Introduction to C
Watch on YouTube →
Overview
Burton Ma closes the Bash unit by explaining why arrays do not pass to functions like ordinary values, showing alternatives including Bash 4.3 name references, then introduces C as a compiled, general-purpose language built for efficiency. He traces C's origins in Ken Thompson and Dennis Ritchie's Unix work, surveys its modern uses, and demonstrates C source files, headers, GCC compilation, preprocessing, `main`, and program exit statuses.
Key takeaways
- Ordinary Bash argument expansion does not pass an array as one array value: `$ARR` passes its first element, while `"${ARR[@]}"` passes separate elements; Bash 4.3 name references provide a by-name alternative.
- C's emphasis on efficiency and programmer control reflects its origins on severely memory-limited computers, including the PDP-7 with roughly 8 KB of memory.
- C remains important in Linux, embedded devices, language runtimes, graphics, games, and computationally intensive software because it offers performance and broad interoperability.
- C source files must be compiled before execution; GCC defaults to creating `a.out`, and edits require recompilation to update the executable.
- In C, `main` returns an integer that becomes a Unix-like process exit status: zero indicates success, while a returned value such as 10 is observable as status 10.
Chapters
- Passing `$ARR` to a Bash function expands to the array's first element, while `"${ARR[@]}"` passes separate elements rather than the array itself.
- One workaround is to pass all array elements and rebuild an array inside the function, though this duplicates unpacking and repacking.
- Bash 4.3 and later supports passing an array by name with a name reference, such as a `local -n` parameter; dynamic scoping can also let a function access an array defined by its caller.
- The in-class test is scheduled for the following Tuesday and allows no aids or cheat sheet.
- Review commands used in assignments, but focus on commonly used options such as `ls` options for listing files rather than memorizing every flag.
- Use the most recent sample midterm as a starting point; older tests remain useful, but select their Bash questions where they also cover C.
- `wc` reports counts such as lines, words, characters, and bytes.
- C is a general-purpose, compiled procedural language, unlike purpose-built languages such as Fortran for scientific computing or COBOL for business applications.
- Like Java, C uses braces, structured control flow, static types, and recursion; Java programmers already know many of C's basic constructs.
- C is considered a low-level high-level language because its operations map relatively directly to hardware capabilities.
- Ken Thompson began developing the B language around 1969; its influence includes the `++` and `--` operators.
- Dennis Ritchie joined Thompson in 1972, and they wrote the Unix kernel in an early version of C; the language was largely completed by 1973.
- C's origins on constrained hardware, including the PDP-7 with about 8 KB of memory, encouraged efficiency and explicit programmer control.
- C changes slowly to preserve compatibility with older code, allowing modern compilers to compile software written as far back as the 1970s.
- C is common in performance-sensitive software, including Linux, embedded systems such as routers, and the implementations of languages such as Python.
- C and C++ are widely used in computer graphics and video games, while computationally intensive applications may use C, C++, or Fortran.
- Visual Studio Code with C extensions is one editor option; Eclipse with C plugins can provide a graphical debugger.
- The course uses GCC for compilation; on Mac systems, a command called `gcc` may invoke Clang or a combination of compiler tools.
- C programs consist of one or more plain-text `.c` source files, optional `.h` header files, and any required libraries.
- A `#include <stdio.h>` directive makes standard input/output declarations available, including `puts` and `printf`; angle brackets designate standard headers.
- `puts` prints a string followed by a newline, while `printf` uses format markers such as `%s` and `\n` to control output.
- C comments use Java-style `//` line comments and `/* ... */` block comments.
- A `.c` file is plain text, not directly executable by Linux; compile it with GCC to create a runnable program.
- Compiling one source file without an output name creates `a.out`; `-o hello` names the executable `hello`.
- After editing source code, recompile before running the executable, or it will still contain the previous version.
- The C preprocessor handles directives such as `#include` before compilation, inserting header contents that commonly contain function declarations.
- A hosted C program needs exactly one definition of `main`; standard forms support either no program arguments or command-line arguments.
- Unlike Java's `main`, C's `main` returns an `int`; in a Unix-like environment, that integer becomes the program's exit status.
- `puts` appends a newline, while `printf` supports formatted output; `\n` represents the newline character.
- Returning `0` signals success, and the demonstration shows that returning `10` produces exit status `10`; omitting the return in `main` also yields zero in the demonstrated environment.
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.