Sunday, February 25, 2018
COMMAND LINE I O IN C
COMMAND LINE I O IN C
command prompt window |
Giving input to program is an important part of any program because after we supply some data then only we can get output based on that input. Let�s take an example:
Suppose you want to find the square of a number.For that you must supply the number-->Input
After the input is give processing takes place.
Finally the square of number is displayed-->output.
After having been understood the need of i/o,let�s take a look at types of i/o.
There are mainly 3 types of i/o :
1.Console i/o2.File i/o
3.Command line i/o
In this very post i will going to discuss about the third type of i/o which is command line i/o.
WHAT IS COMMAND LINE I/O?
To execute a program, first we have to compile it and then we run the program. This work can be minimised by giving the input at the time of execution of program without program prompting us to give the i/p.So we supply the i/p at the command prompt itself.
WHAT ARE COMMAND LINE AGRUMENTS?
These are the arguments that we pass on to the
main() at the command prompt.
main() have 2 arguments:
1.argc: It is int type.It is equal to number of strings to which argv points.
2.argv: It is an array of pointers to strings.
WHAT IS THE SYNTAX OF main() with command line arguments?
void main(int argc,char *argv[])Let�s take a simple example of copying a file through the command prompt.
From the above you could make out that we have to just give the source file name(source.txt) and target file name(target.txt).
Let�s take one more example which will make this concept much more clear to you.
EXAMPLE:
Input 2 numbers and find out which one is greater.
void main(int argc, *argv[])
{
if(argc.length<3)}
{
printf(� syntax is: <file name> <num1> <num2>�);
}
else
{
if(argv[0]>argv[1])
{
printf(�Num1 is greater�);
}
else
{
printf(�Num2 is greater�);
}
}
getch();