Computer Science‎ > ‎

C Program: The Usage of Command Line Arguments


C Program demonstrating the use of Command Line Arguments

/*A C program is executed as if it is a function called by the Operating System, the Operating System can and does pass parameters to the program.
There are two parameters. These two parameters fully specify the command line that was used to invoke the executing program.
The first parameter is argc. It is an integer that represents the number of white space separated strings on the command line. 
For example if we execute program learn , where we pass command line arguments as
learn hello, world then the argc=3
The second parameter is argv.
It is an array of pointers to strings. The strings(represented in a charcter array form) and the array pointer(argv) points to the first string.
If a program is executed (invoked) with a command line that looks like
learn hello, world
then the value of argc is 3 (three), and argv can be diagrammed as
  argv
  ---         ---        -------------------------
 |  -|---->  |  -|----> | l | e | a | r | n |\0 |
  ---        |---|       -------------------------
             |  -|----> | h | e | l | l | o | , |\0 |
             |---|       -------------------
             |  -|----> | w | o | r | l | d |\0 |
             |---|       ---------------------------
            

Each of the values within the boxes to the right (representing each string) is a single character. 
argv[0] is the (pointer to the) string "learn".
argv[1] is the (pointer to the) string "hello,".
argv[2] is the (pointer to the) string "world".

In addition, for ANSI standard C, the array element argv[argc] exists, and is defined to be a null pointer. Therefore, the diagram for the example command line given will be:

 argv
  ---         ---        -------------------------
 |  -|---->  |  -|----> | l | e | a | r | n |\0 | This is can also be pointed to by argv[0]
  ---        |---|       ----------------------------
             |  -|----> | h | e | l | l | o | , |\0 | This is can also be pointed to by argv[1]
             |---|       ----------------------------
             |  -|----> | w | o | r | l | d |\0 | This is can also be pointed to by argv[3]
             |---|       ---------------------------
             | 0 | This points to NULL
             |---|
 */
#include<stdio.h>
int main(int argc,char *argv[])
{
    int i;
    //Displaying the value of the argc
    printf("%d\n",argc);
    //loop to siplay all the arguments passed after the program name on separate lines
    for(i=1;i<argc;i++)
    printf("%s%s",argv[i],"\n");//Displaying the argument first and then changing the line
    getch();
}
/*
A test run on the program was carried out with arguments being passed as commandline Learning Point is Awsum.commandline here is
the name of the program and rest of the arguments are diplayed indivsually on separate lines after displaying hte number of arguments(argc).
Hence the resulting output was
5
Learning
Point
is
Awsum
*/








 















C Program to Reverse A String

C Program: Building an Expression Evaluator
C Program: Check for Armstrong Numbers
C Program: Check whether a string is a Palindrome or not
C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product
C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions
C Program: Computing the Area of a Circle
C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix
C Program: Demonstrating File Handling Functions
C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants
C Program: Demonstrating the use of Bitwise Operators
C Program: Displaying a Histogram of word frequencies (unigram)
C Program: Distance Vector Routing Algorithm using Bellman Ford's Algorithm
C Program: Numerical Computing - The Gaussian Elimination Method
C Program: Numerical Computing - Implementing the Newton Raphson Method
C Program: Numerical Computing - the Bisection Method
C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra
C Program: Numerical Computing - the Jacobi Method
C Program: Printing the Pascal Triangle
C Program: Reversing the order of words in a sentence
C Program: Solving Simultaneous Equations in Two Variables
C Program: Source Code for computing the GCD(HFC) of two numbers
C Program: Source Code for Solving Quadratic Equations
C Program: Source code to solve the Josephus Problem
C Program: Sudoku Solver
C Program: The Usage of Command Line Arguments
C Program: Using the Sieve of Eratosthenes to print Prime Numbers