Skip to content
Learn Netverks

Lesson

Step 30/36 83% through track

command-line-args

Command-line arguments

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Command-line arguments: the syntax, patterns, and safety habits you need before advancing in C.

Teams still ship Command-line arguments in C codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Command-line arguments in contexts like: Kernels, drivers, embedded devices, and performance libraries used by other languages.

Write C in main.c with int main(), click Run on server—the dev runner compiles with cc/gcc -std=c11 and runs the binary; read stderr for compile and linker errors (LEARNING_RUNNER_ENABLED=true).

Toward the end of the track—consolidate before capstone-style review lessons.

Programs started from a shell receive argc (argument count) and argv (argument vector). argv[0] is typically the program name.

Signature

int main(int argc, char *argv[]) {
    for (int i = 0; i < argc; i++) {
        printf("argv[%d] = %s\n", i, argv[i]);
    }
    return 0;
}

Run locally: ./main hello world passes two extra strings.

Parsing tips

Validate argc before accessing argv[1]. Use strtol for numeric args with error checking.

Important interview questions and answers

  1. Q: Type of argv?
    A: Array of pointers to null-terminated char strings—char ** equivalently.
  2. Q: argc value with no args?
    A: At least 1—program name alone.

Self-check

  1. What is argv[0] usually?
  2. How do you avoid accessing argv[1] when argc is 1?

Tip: Always guard argv[1] with argc > 1—accessing missing args is out-of-bounds undefined behavior.

Interview prep

argc minimum?

At least 1 when invoked—argv[0] is typically the program name.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • argv[0] is?
  • argc zero when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump