command line arguments
What is command line arguments? how should i declare it? what is its use?
Collapse
X
-
-
Command line arguments are used when you run your program from the OS command prompt:
c:\>myprog.exe filename.txt
Here a program named myprog has an argument that is a file name. Maybe the program will read or write the file. Without this, the program would need you to enter the file name as part of the program.
Basically, any data the program needs in order to run can be passed as a command line argument. These arguments are passed to main:
argc is the number of arguments. In our example, there are 2 arguments:mypro g.exe and filename.txt. You use argc to tell you which arguments you are using:Code:int main(int argc, char* argv[]) { }
argv[0] is myprog.exe
argv[1] is filename.txt
In the program if you fopen argv[1] you will be opening filename.txt. -
only the integer and a character pointer can be passed through the main function?Command line arguments are used when you run your program from the OS command prompt:
c:\>myprog.exe filename.txt
Here a program named myprog has an argument that is a file name. Maybe the program will read or write the file. Without this, the program would need you to enter the file name as part of the program.
Basically, any data the program needs in order to run can be passed as a command line argument. These arguments are passed to main:
argc is the number of arguments. In our example, there are 2 arguments:mypro g.exe and filename.txt. You use argc to tell you which arguments you are using:Code:int main(int argc, char* argv[]) { }
argv[0] is myprog.exe
argv[1] is filename.txt
In the program if you fopen argv[1] you will be opening filename.txt.Comment
Comment