Anything typed after the program name arrives in main as argc and argv — a count and a list of words.
1 main gets extra info
main(int argc, char *argv[]) receives what the user typed after the program name.
2 argc counts
argc is the number of words on the command line, including the program name itself.
3 argv holds the words
argv[0] is the program name; argv[1], argv[2]... are the arguments the user typed.
4 Everything is text
Arguments arrive as strings — use atoi (from stdlib.h) to turn "25" into the number 25.
main gets extra info
main gets extra info1main(int argc, char *argv[]) receives what the user typed after the program name. argc counts2argc is the number of words on the command line, including the program name itself. argv holds the words3argv[0] is the program name; argv[1], argv[2]... are the arguments the user typed. Everything is text4Arguments arrive as strings — use atoi (from stdlib.h) to turn "25" into the number 25. 4 building blocks make up main gets extra info
Press Run and follow the words you typed on their way into your program.
Press play — the program was started as ./add 3 4, so the words arrive before main even runs.
preprocessThe preprocessor pastes in stdio.h and stdlib.h — atoi comes from stdlib.h.
Memory
no variables yet — press play
Console
output appears as the program runs…
Never trust the user — count the words before reading them.
process decision input / output start / end The program is run as ./double 21 — what does it print?
Your predicted output — one line per console line
Hint: argc is 2, argv[1] is the text "21", and atoi turns it into the number 21.
01argc counts every word on the command line — the program name included.
02argv[0] is the program name; user arguments start at argv[1].
03Arguments arrive as strings — atoi converts them to numbers.
04Check argc before touching argv[k] — a missing argument means a crash.
05Returning 1 (not 0) signals failure to scripts and the shell.