×

Command line arguments in C

Command line arguments in C

The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by the main() functions. In order to support this command line prompt in the C standard, the developer has to change the structure of the main() function of the program or application.

The command  line arguments are considered as a parameter that is supplied to the program every time it is invoked. They are an important part of the C programming language and hence are mostly used when one needs to control the programs from the outside rather than from the inside.

C provides programmers to put command-line arguments within the program, which will allow users to add values at the very start of program execution. These command line arguments are often given the name of the application or the program in the operating system like DOS( Disc Operating System) or Linux.

Learning about the declaration of the main() function is more important which has no accepted arguments in the previous times. The main() function can accept two types of arguments from the command line, one being the number of command line arguments and other being the full list of all the arguments of the command line.

Syntax:

             int main(int argc, char *argv[])
 Where, 
  • argc- The file name is the first argument, and it counts the number of arguments on the command line. argc is an integer (int) and it stores the name of the program. So if the value of the program is being passed, the value of the argc would be 2 as one will be considered for the argument and the other for the program name. The value of the argc should always be a negative value.
  • argv[]- is a pointer that consists of the total number of arguments present in the program and the first argument is the name of the file. In other words Argv[] is an array of characters which has all the arguments.
  • If the value of the argc is greater than the value of 0, then the array elements from the argv[0] to argv[argc-1] will contain pointers to strings.

When C programs or applications are executed, it is possible to send values from the command line to them. These values are known as command line arguments, and they are frequently required by programmers and developers. The most important function of the C programming language is the main() function. It is mostly defined with a return type of int and without parameters:

 int main()
 {
  /* ... */
  } 

When C programs are being executed, it is possible to send values from the command line to them.

E.g.:

 #include <stdio.h>
 #include <conio.h>
 int main ( int argc, char *argv[] )
 {
 if ( argc != 2 ) /* argc should be 2 for correct execution */
 {
 /* We print argv[0] assuming it is the program name */
 printf( "usage: %s filename", argv[0] );
 }
 else
 {
 FILE *file = fopen( argv[1], "r" );
 /* fopen returns 0, the NULL pointer, on failure */
 if ( file == 0 )
 {
 printf( "Could not open file\n" );
 }
 else
 {
 int x;
 while  ( ( x = fgetc( file ) ) != EOF )
 {
 printf( "%c", x );
 }
 fclose( file );
 }
 }
 } 
 #include <stdio.h>
 #include <conio.h>
 int main(int argc, char *argv[])
 {
 int i;
 if( argc >= 2 )
 {
 printf("The arguments supplied are:\n");
 for(i = 1; i < argc; i++)
 {
 printf("%s\t", argv[i]);
 }
 }
 else
 {
 printf("The argument list is empty.\n");
 }
 return 0;
 } 

Output:

The argument list is empty.

Remember that argv[0] holds the name of the program and argv[1] points to the first command line argument and argv[n] gives the last argument. If no argument is supplied, argc will be 1.

Properties of Command Line Arguments:

  1. They are always passed to the main() function.
  2. Argv[argc] is considered to be a NULL pointer .
  3. They are parameters or the arguments supplied to the program when it is invoked.
  4. They are used to control programs from outside instead of hard coding those values inside the code.
  5. argv[0] holds the name of the program.
  6. argv[1] points to the first command line argument and argv[n] points to the last argument.

You pass all the command line arguments separated by a space, but if the argument itself has a space then you can pass such arguments by putting them inside double quotes “” or single quotes ”.


Related Topics

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

C Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

Infix to Postfix program in C

Infix Expression: In infix expression, an operator is placed between the two operands. Example: x + y, here operator + is placed between operands x and y. Postfix Expression: In...

3 minutes read.

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

function pointer as argument in C

Pointers are considered difficult to understand for beginners but pointers can be made to work if you fiddle with them long enough. So, let’s understand this step by step. What are...

3 minutes read.

Static function in C

The functions in the C programming language are by default global. This means the programmer can easily access the function which is outside from the file where it was initially...

3 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Continue in C

C language: C language is a procedure oriented programming language. We can say that it is a platform dependent language. C language is introduced by Dennis Ritchie in the year 1970. We...

2 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.