×

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

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 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.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Pure Virtual Function in C

Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below. In c++ the member function that is defined in the...

4 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

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.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.