facebook like button

06 June, 2011

program 61: command line arguments demo

The code:
--------------------------------------------
#include<stdio.h>
int main(int argc,char *argv[])
{
    int i=0;
    printf("total number of arguments given=%d\n",argc);
    printf("Executing program name is %s\n",argv[0]);
    printf("Other arguments given are as under:\n\n");
    while(argv[i++])
    printf("%s\n",argv[i]);
    return 0;
}
--------------------------------------------
Understanding execution:
First compile the program. People who are using C-free or other compiler which does not asks command from user and directly runs the program with a single click should only compile the code with that compiler. In this way an .exe file will be created in the same directory. Now open command prompt and run program there. Steps for C-free are:
1. write the code and save it with any name say prog1.c.
2. Go to build menu and select compile or press f11.
3. Again go to build menu and select make or press Shift+f11.
4. Now see a prog1.exe file is created in the same directory.
5. Open command prompt program form "all programs->accessories->command prompt".
6. navigate to your program directory.
7. now run the command:
prog1.exe ranu pratap singh
and you are done with program.
The output:
total number of arguments given=4
Executing program name is temp.exe
Other arguments given are as under:

ranu
pratap
singh
(null)

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply