Building From the Command Line |
Introduction |
Most of the programs created in MS Visual C++ are also easily built, compiled, and executed inside of MS Visual C++. Sometimes, you will need to execute a program from the DOS prompt. This exercise simply shows how to compile a program from the command line. By default, the command line is not ready to compile programs after MSVC has been installed. You must explicitly make this available. |
If you haven't allowed done so, follow the following step to allow command lines.
|
Once the command line utility is setup, you can create your projects.
|
The main() Function and Arguments |
The C++ Standard allows the main function to have or not have arguments. This allows the programmer to have some control on the behavior of the main() function and even the program as a whole. You can use one or more arguments on the main function. Here is the general formula used: int main(int argc, char *argv[]) The first argument, argc, specifies how many arguments will be passed to the main() function. The actual arguments are passed as an array of strings. Each string represents its own value. Because this argument is made of strings, you are partly in control of what the arguments can be. In this syntax above, keeping in mind that argc represents the number of arguments, the main() function itself is the first argument at index 0. Therefore, argc is always at least 1. Its total number is from the elements of the argv array. This means that the value of argv[0] is main. If the user types anything after the name of the program on the command prompt, what follows the name of the program is argv[1]. For example, if the program is called Exercise1. Imagine that the program has been compiled. If the user types: C:\MyPrograms\Exercise1 68 the value 68 would be argv[1] Depending on how you write your program, the user can also be allowed to type more than one value. An example would be: C:\MyPrograms\Exercise1 68 1250.50 Because the members of the argv argument are strings, it is your responsibility to get them and convert them to the desired value. This means that, just because the user types C:\MyPrograms\Exercise1 68 1250.50 doesn't mean that 68 is an integer and that 1250.50 is a double. Of course, the C\C++ compiler provides you with conversion functions. To convert a value such as 68 to an integer, you can use the atoi() function. In the same way, you can convert a value such as 1250.50 to double using atof(). If the user would type a character, the value should be provided in single-quotes. If the value is a string, the user must type it in double-quotes. Using these techniques, the user can be allowed to provide numeric, characters, or string values.
|
|
||
Home | Copyright © 2003-2012 FunctionX | |
|