C Tutorial

C Command Line Arguments

main can take argc and argv. They are how a program reads flags typed after its name.

A different shape of main

Until now this track has used int main(void). That form takes no arguments. To read words typed after the program name, declare int main(int argc, char *argv[]).

argc is the count of those words, including the program name. argv is an array of C strings. argv[0] is the program name (as the system reports it). argv[1] is the first extra argument, if any. The last used index is argc - 1.

Print argc and argv[0]

This program reports how many arguments arrived and prints the first string. On a local machine you might run ./prog hello and see argc equal to 2. In the StudyGrid editor the picture is smaller.

Example

#include <stdio.h>

int main(int argc, char *argv[]) {
  printf("argc: %d\n", argc);
  printf("argv[0]: %s\n", argv[0]);
  return 0;
}

Compile this at /c/try. That editor is gcc. The browser typically runs the program with argc == 1: only the program name, no extra words after it.argv[0] still prints. There is no field in the editor for extra command-line arguments.

Walk every argument

Loop from 0 to argc - 1 and print each string. Locally, extra arguments show up as later indexes. In the browser you usually get one line: index 0 and the program name.

Example

#include <stdio.h>

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

Do not read argv[argc] as a useful string. The standard says that slot is a null pointer. Stay in the range 0 .. argc - 1 when you print or copy arguments.

What each piece means

NameMeaning
argcHow many strings in argv. Always at least 1.
argv[0]The program name, or a path to it.
argv[1]The first extra argument, if argc > 1.
argv[argc]A null pointer. Do not print it as a string.

Arguments are text. If you need a number, convert the string with atoi fromstdlib.h, or (better) strtol, after you have checked that the argument exists. The Type Conversion chapter covers casts; converting text to an integer is a library call, not a cast.

void versus argc/argv

Keep int main(void) when the program does not read the command line. Useint main(int argc, char *argv[]) when it does. Both return int. Both shouldreturn 0 on success. Next: time.h, for measuring clock time instead of reading arguments.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.

Physics

Metres to centimetres

There are 100 cm in a metre. 2.50 m is 250 cm. Command-line tools take the number after the program name. /c/try may not pass argv, so the default 2.5 still runs.

cm = m × 100

Example

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  double metres = 2.5;
  if (argc >= 2) {
    metres = atof(argv[1]);
  }
  printf("%.2f m = %.1f cm\n", metres, metres * 100.0);
  return 0;
}

Chemistry

Moles from a mass

n = m / M. 18 g of water is 1.00 mol if M is 18 g/mol. Pass a different mass on the command line when you have a shell. atof converts the text "36" into 36.0.

n = m / M

Example

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  double grams = 18.0;
  const double molar_mass = 18.0;
  if (argc >= 2) {
    grams = atof(argv[1]);
  }
  printf("n = %.3f mol\n", grams / molar_mass);
  return 0;
}

FAQ: C Command Line Arguments

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c command line arguments examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c command line arguments in this C C lesson (C Command Line Arguments).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.