argc and argv, at termination through the return value of main, and through the shell with environment variables. Heads up: today is also Quiz 8 (File I/O) and the Team PEX is assigned.
By the end of this lesson, cadets will be able to:
argc and argv. (Outcome 2)main's return value to communicate program status to the calling shell. (Outcomes 2, 7)getenv to let the runtime environment configure program behavior. (Outcomes 2, 7)Complete before class. These set the vocabulary we will use.
| Source | Sections | Why |
|---|---|---|
| Beej-C | Chapter 18 (The Outside Environment) | argc, argv, exit status, environment variables |
char **argv alternate notation) for now; we will revisit the equivalence in slides.argc? What is each cell of argv (including the sentinel at the end)? Try the in-class walkthrough widget to confirm.Three ways the world talks to your program. Read each subsection below; the slides cover the same material at the front of class, and the walkthrough widget gives you hands-on practice with each one.
The Lesson 22 deck covers argv shape, parsing and validating, exit status, environment variables, and a brief glance at getopt as the standard idiom you will meet again in CS220.
When the shell launches your program, it hands main two parameters. The signature changes from the int main(void) you have been writing to:
If you run ./prog hello 42, then argc is 3 (the program name counts) and argv looks like this:
| Index | Value (a string) |
|---|---|
| argv[0] | "./prog" |
| argv[1] | "hello" |
| argv[2] | "42" // still a string, not a number |
| argv[3] | NULL // sentinel; guaranteed by the standard |
Quoting groups: ./prog "hello world" 5 gives argc=3, not argc=4. The shell sees the double quotes and packs the whole phrase into one argv cell.
argv elements are strings. To use them as numbers, you convert:
The trap: atoi returns 0 for both "0" and "banana". There is no error flag. For inputs you cannot trust, strtol reports parse failures through an end-pointer and errno. For CS210 labs, atoi is usually fine; the lab spec will tell you when validation is part of the grade.
Validate before you index. Always check argc before reading from argv. The pattern:
Usage messages and errors go to stderr, not stdout. The reason: if your program is piped to another (./prog file.txt | sort), stdout becomes the next program's input. Errors on stdout would corrupt the data; errors on stderr still land on the terminal where the user can see them.
The integer your main returns is captured by the shell as the program's exit status. Zero means success; any non-zero value means failure. The <stdlib.h> macros make the intent obvious:
After your program ends, the shell exposes the exit code as $?:
Beyond what gets passed at launch, the shell also gives your program a set of environment variables: key-value pairs like USER=cadet22 and HOME=/home/cadet22 that any process inherits. You read them with getenv:
getenv returns a pointer to the value string, or NULL if the variable is not set. Always check for NULL before using the result. The returned string belongs to the environment, not to you, so do not free it and do not modify it.
Why this matters: environment variables let you change your program's behavior without recompiling. The lab uses STRAVA_QUIET as a small example: when set, the filter suppresses its summary line.
Real Unix command-line tools (grep -i pattern file, gcc -Wall -Werror file.c, ls -la) accept option flags in standard shapes. The standard library function that recognizes these patterns is getopt. A taste:
You will not write getopt in CS210; the lab uses simple positional arguments. But you will see this idiom in any real command-line tool you read, and in CS220.
Four things to do in class. Use the checklist to track your progress.
Twelve tasks across four sections. Type each shell invocation or C call at the prompt. The terminal shows what happened; the right pane shows the current argv array, exit status, and any environment variables you read.
Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Lesson 22 reflection.
./prog hello 42 and watch the resulting argv array appear. Some tasks ask you to type a C statement instead (like atoi(argv[1])) to see what would happen inside main. Only the expected input will advance the walkthrough. Mistype twice and the hint button will pulse. Free mode unlocks at the end.
Eight questions. Pass at 70% or above; below that, take it again after reviewing the materials and replaying the walkthrough.
Enter your name. It will appear on the score card so you can include it in your screenshot for the Lesson 22 reflection.