There are methods to interact with a program in command line itself,by command line i mean the terminal by which the program can be executed.Is it possible to supply arguments to a program in the command line ?
YES !,
we first learn to compile and run a program using gcc
say you have a program hello.c
to compile we use gcc -c hello.c or a simple gcc hello.c would do
The above command creates a default object file by the name a.out so if you wanna execute the program you need to ./a.out and hit enter
but if you want to give your name to the object file then you can do so by
gcc hello.c -o yourname
the program then can be run by just ./yourname
NOW FOR THE COMMAND LINE ARGUMENT PASSING
lets say you made a object file name by using gcc prog.c -o name
now to pass arguments to this program prog from the command line ,The prog program would have
int main(int argc,char * argv[])
{ .......
........
........
}
int argc indicate how many arguments are passed and argv will store the arguments
that is if you want to pass a alist of arguments to your prog program with object name then
./name prog.c 25 wow! 56.78 this$is$a$string
now observe the syntax i wrote ./object (name of the program ie prog.c) (argument list)
the argument list is seperated by integers and the first is an integer .2nd a string ,3rd a float and the 4th is a string but i have used delimiters such as $ to indicate space..and its your job to replace the $ with space inside the program.
you could have used any other delimiter apart from $
now inside the program the argc is holding value 5
and argv[0] = "prog.c"
argv[1] = 25
argv[2] ="wow!"
and so on
Thats about it ,this provides a simple enough usage of command line argument passing
YES !,
we first learn to compile and run a program using gcc
say you have a program hello.c
to compile we use gcc -c hello.c or a simple gcc hello.c would do
The above command creates a default object file by the name a.out so if you wanna execute the program you need to ./a.out and hit enter
but if you want to give your name to the object file then you can do so by
gcc hello.c -o yourname
the program then can be run by just ./yourname
NOW FOR THE COMMAND LINE ARGUMENT PASSING
lets say you made a object file name by using gcc prog.c -o name
now to pass arguments to this program prog from the command line ,The prog program would have
int main(int argc,char * argv[])
{ .......
........
........
}
int argc indicate how many arguments are passed and argv will store the arguments
that is if you want to pass a alist of arguments to your prog program with object name then
./name prog.c 25 wow! 56.78 this$is$a$string
now observe the syntax i wrote ./object (name of the program ie prog.c) (argument list)
the argument list is seperated by integers and the first is an integer .2nd a string ,3rd a float and the 4th is a string but i have used delimiters such as $ to indicate space..and its your job to replace the $ with space inside the program.
you could have used any other delimiter apart from $
now inside the program the argc is holding value 5
and argv[0] = "prog.c"
argv[1] = 25
argv[2] ="wow!"
and so on
Thats about it ,this provides a simple enough usage of command line argument passing
No comments:
Post a Comment