Command Line Arguments

Raheelanjum
3 min read2 days ago

Some C programs can behave in many different ways, based on the user's request. For example, if we use the "ls" command to list files in a directory, we get one format of data back. If we use "ls -l," we get a long listing, which is a different format. The "-l" is considered a command line argument. The C program must "parse" the command line arguments (which are given to the program as a list of strings) and transform this type of data into a form useful in "guiding" the program to execute in the manner specified by the user.

Command Line Args

Many programs have command line args that tell the program how to behave:

In C, when you run a program via the Linux command line, you can look at these command line values and alter the behavior of your program as well.

The first step to tell a C program to "get" the command line values is to change the signature of the main function, as follows:

number_of_args: the total number of "values" on the command line.

Note: The name of the program is counted and is the first value.

--

--

Raheelanjum
Raheelanjum

Written by Raheelanjum

Highly skilled and experienced content writer dedicated to crafting compelling, informative content that resonates with diverse audiences. Expertise spans blog

Responses (29)