Wednesday, 10 September 2014

Command line argument in C Language Program with example

Command line argument in C: An example C program is available here. Given source code is based on command line operations by passing arguments with the advantage of arrays. Sample code is also given below for your reference.

COMMAND LINE ARGUMENT IN C
This C program is used to pass the arguments as the command statement. Declare the variables and pass the value by using a command format of statement.


//Header files
#include<stdio.h>
#include<conio.h>
//Global variables
int main(int ar, char *ar[])
{
    int ct; //Program variables
    clrscr(); //Function to clear previous output
    printf("Program name: %s\n", ar[0]); //Display function
    if(ar>1//Conditional statement
    {
        for(ct=1; ct<ar; ct++) //Looping statement
                  printf("Argument %d: %s\n",ct,ar[ct]);
    }
    else
        puts("No command line argu entered.");
    getch();
    return 0;
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.