Skip to content

Expanded Source Commentary

Scover edited this page Apr 7, 2025 · 2 revisions

Expanded Source Commentary

This page contains an annotated, valid version of argc.c, where each C token is explained line by line.

Use it to understand the anatomy of a command-line utility—or just to enjoy the ridiculousness of it all.

argc-annoated.c: argc's source, annotated

#include <stdio.h>  /* The '#include' directive tells the compiler to copy the contents of a file here, before compilation.
                       This is part of the *preprocessing* step. We include the Standard Input/Output library, which defines `printf`.
                       Angle brackets <...> mean “look in the system include paths” (like standard library headers). */

int                 /* The function returns an integer. In C, this value is used as the program's exit code. By convention, returning 0 means "success". */
main                /* The entry point of every C program. It must be named 'main'. */
(                   /* Start of the parameter list for the function */
    int argc        /* First parameter: the number of command-line arguments passed to the program.
                      This always includes the program name itself. */
    ,               /* Comma separates function parameters. */
    char **argv     /* Second parameter: an array of strings, one for each argument.
                      'char *' is a string (a pointer to characters), so 'char **' means "pointer to a string pointer",
                      i.e., an array of strings.
                      We don't use 'argv' in this program, but it must still be declared for compatibility. */
)                   /* End of the parameter list */
{                   /* Start of the function body */

    printf          /* 'printf' is used to write formatted output to the screen (stdout = standard output). */
    (               /* Start of the function argument list */
        "%d\n"      /* The format string: '%d' says "insert an integer", and '\n' is a newline character. */
        ,           /* Comma separates arguments to the function. */
        argc        /* The actual value to print: the number of command-line arguments. */
    )               /* End of the argument list */
    ;               /* Every statement in C ends with a semicolon.
                      (Not to be confused with the Greek question mark: `;`) */

    /* Note: In C, if 'main' reaches the end without an explicit 'return', it *implicitly* returns 0 (success).
       However, this is only true since C99. Writing 'return 0;' is clearer and more portable. */

}                   /* End of the function body */

/*
 * GLOSSARY
 *
 * array of strings
 *                A list of text values. Represented in C as `char **argv`.
 *                Often used to store and access command-line arguments.
 *
 * argument       A value passed to a program from the command line when it starts.
 *                For example, in `./argc foo bar`, the arguments are "foo" and "bar".
 *
 * argc           The "argument count" — the number of command-line arguments passed to `main`.
 *                This always includes the program name itself as the first argument.
 *
 * argv           The "argument vector" — an array of strings representing each command-line argument.
 *
 * main function  The entry point of every C program. This is where execution begins.
 *
 * pointer        A value that stores a memory address. In C, `char *` is a pointer to a character.
 *                `char **` is a pointer to a pointer — commonly used for arrays of strings.
 *                So if argc is 3, argv might be: ["./argc", "foo", "bar"].
 *
 * stdout         The default output stream in C. Normally, this refers to the terminal or console.
 */

Clone this wiki locally