student@ubuntu:~$
c 2/5 25 XP

CLI Arguments

0%

Quick Reference

argc/argv basics:

// ./prog hello 42
// argc = 3
// argv[0] = "./prog"
// argv[1] = "hello"
// argv[2] = "42"
// argv[3] = NULL

Conversion functions (from <stdlib.h>):

Function Converts to Example
atoi(s) int atoi("42") = 42
atof(s) double atof("3.14") = 3.14
strtol(s, NULL, 10) long (safer) Detects errors

Common Pitfalls

  • No argc check – Accessing argv[1] without verifying argc >= 2 segfaults when no args are given.
  • Forgetting conversion – argv values are strings. argv[1] + 1 does pointer arithmetic, not math.
  • Off-by-one – argv[0] is the program name. User arguments start at argv[1].

Unlocks

Complete this skill to see what it unlocks.