Skip to content

How to access the command line parameters in C

New Course Coming Soon:

Get Really Good at Git

Learn how to access the command line parameters in C

In your C programs, you might have the need to accept parameters from the command line when the command launches.

For simple needs, all you need to do so is change the main() function signature from

int main(void)


int main (int argc, char *argv[])

argc is an integer number that contains the number of parameters that were provided in the command line.

argv is an array of strings.

When the program starts, we are provided the arguments in those 2 parameters.

Note that there’s always at least one item in the argv array: the name of the program

Let’s take the example of the C compiler we use to run our programs, like this:

gcc hello.c -o hello

If this was our program, we’d have argc being 4 and argv being an array containing

Let’s write a program that prints the arguments it receives:

#include <stdio.h>

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

If the name of our program is hello and we run it like this: ./hello, we’d get this as output:


If we pass some random parameters, like this: ./hello a b c we’d get this output to the terminal:


This system works great for simple needs. For more complex needs, there are commonly used packages like getopt.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!
→ Get my C Handbook

Here is how can I help you: