2.3 - Basics (2026-08-31)
Overview
Chris Bourke teaches C input and output, moving from `printf` formatting and interactive `scanf` to command-line arguments for non-interactive programs. He demonstrates `argc` and `argv`, converting argument strings with `atoi` and `atof`, validating input, and building a quadratic-roots program with the math library.
Key takeaways
- `printf` precision controls displayed decimal places and rounds only the formatted output; it does not modify the underlying floating-point value.
- `scanf` requires addresses such as `&x` because it writes input into existing variables, and `%lf` is the demonstrated placeholder for reading a `double`.
- Command-line arguments are strings, and `argc` includes the executable name, so a program expecting one user value should check for `argc == 2` before accessing `argv[1]`.
- The miles-to-kilometers example shows that checking argument counts and exiting after an error prevents missing input from causing an invalid memory access.
- A quadratic solver should check that `a` is nonzero and that `b² − 4ac` is nonnegative before using the real-valued quadratic formula.
Chapters
- After a brief musical opening, Chris Bourke resumes the C basics lesson by reviewing standard output through `printf`.
- The examples use a terminal for standard output, while a grader can display program output through a web interface.
- The `printf` placeholders `%f`, `%d`, and `%c` format doubles, integers, and characters; multiple placeholders match arguments in order.
- For floating-point output, `%.4f` prints four digits after the decimal and rounds the displayed value without changing the stored number.
- A format such as `%10.4f` sets a minimum field width of 10 and a precision of 4; `%−10.4f` left-aligns the result.
- For a six-digit integer printed in an eight-character field, `%8d` adds leading spaces and `%08d` adds leading zeros.
- Writing a leading zero in an integer literal, such as `0123456`, makes it an octal value in C rather than a decimal number with padding.
- The lesson also notes that commas, spaces, and underscores cannot be used as digit separators in the demonstrated integer literal.
- `scanf` reads interactive keyboard input; the examples use `%d` for integers, `%c` for characters, and `%lf` for doubles.
- Variables passed to `scanf` need the address-of operator, as in `scanf("%lf", &miles)`, so the function can store input in the variable.
- Omitting `&` can trigger a compiler warning and produce incorrect results or a crash, so Bourke advises addressing warnings rather than ignoring them.
- C's `argc` counts command-line arguments, including the executable name at `argv[0]`; the first user-provided value is at `argv[1]`.
- Values such as `42` and `3.5` arrive as strings, so the examples use `atoi` to convert to an integer and `atof` to convert to a double.
- The demonstration converts `argv[1]` and `argv[2]` into numeric variables before using them in calculations.
- Bourke converts an interactive miles-to-kilometers program to use a command-line value, parsing miles as a double with `atof(argv[1])`.
- Running the program without the required value can cause a segmentation fault because the code tries to access a missing argument.
- Checking that `argc` equals 2, printing an error, and exiting with a nonzero status prevents the program from continuing with missing input.
- The exercise computes roots for `ax² + bx + c = 0` by reading coefficients `a`, `b`, and `c` rather than a root value.
- The program scaffold includes `stdio.h`, `math.h`, and `main(int argc, char **argv)`; the expected argument count is 4, including the executable name.
- Because coefficients can be fractional, Bourke uses `double` variables and `atof` to parse `argv[1]`, `argv[2]`, and `argv[3]`.
- The solver calculates two roots using the positive and negative forms of the quadratic formula and prints the coefficients and results.
- Programs using math functions such as `sqrt` need to link the math library with `-lm` in the demonstrated GCC environment.
- Bourke tests coefficients against Wolfram Alpha, adjusting the values when the discriminant `b² − 4ac` is negative and the roots are complex.
- If `a` is zero, the equation is linear rather than quadratic; the program should detect this before applying the quadratic formula.
- A negative discriminant produces complex roots, which the example program does not handle; a zero discriminant is valid and yields a repeated real root.
- The closing review covers checks for missing arguments and invalid coefficients, previewing the `if`, `else if`, and `else` statements planned for the next class.
Transcript excerpt
0:03 [music] She should stay away from friends. She [music] should have had more time to spare. She should have died when she was born. She should have [music] won a crown of thorns. Sh. [music] [music] She should [music] understand. She should have another chance. Heat. Heat. [music] [music]
0:58 Heat. [music] [music] Heat. [music] She should [music] stay away from friends. She should have had more time to [music] spend. She should die when she was born. She want a crowd of she. [music] [music]
2:00 All right. So on Friday, uh today we'll uh finish up talking about the basics. uh so that we can move on uh to conditionals on Wednesday. On Friday, we left it off with basic input and output. And we started with input. Uh print f is the function that you want to use to print anything to the standard output, which for us is going to be our uh little terminal over here, right? Uh but of course, when you go to the grader, it'll also have standard output. Uh but when when you look at it in u with respect to like grade scope, it'll actually output it to your web uh to your canvas instance on your web browser. Uh percent f is used for doubles to format a double. That's because f stands for a floatingoint number. Uh percent d is for integer values. It stands for digit. And the
2:47 only one that makes sense is percent c for character values. So if I have three variables here, then I can print them out all at once, right? I can provide multiple uh placeholders at once. It's always percent. And so the for the first one that will be associated with this guy over here. The second one will be associated with this guy over here, etc.
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, Chris Bourke.