Earlier, you found that the main function returned a value and by default this value is zero. When your application finishes, you can return an error code back to the command line; this is so that you can use the executable in batch files and scripts, and use the value to control the flow within the script. Similarly, when you run an executable, you may pass parameters from the command line, which will affect how the executable will behave.
Run the simple application by typing the simple command on the command line. In Windows, the error code is obtained through the pseudo environment variable ERRORLEVEL, so obtain this value through the ECHO command:
C:\Beginning_C++\Chapter_01>simple
Hello, World!
C:\Beginning_C++\Chapter_01>ECHO %ERRORLEVEL%
0
To show that this value is returned by the application, change the main function to return a value other than 0 (in this case, 99, shown highlighted):
int main()
{
std::cout << "Hello, world!n";
return 99;
}
Compile this code and run it, and then print out the error code as shown previously. You will find that the error code is now given as 99.
This is a very basic mechanism of communication: it only allows you to pass integer values, and the scripts that call your code must know what each value means. You are much more likely to pass parameters to an application, and these will be passed through your code via parameters to the main function. Replace the main function with the following:
int main(int argc, char *argv[])
{
std::cout << "there are " << argc << " parameters" <<
std::endl;
for (int i = 0; i < argc; ++i)
{
std::cout << argv[i] << std::endl;
}
}
When you write the main function to take parameters from the command line, the convention is that it has these two parameters.
The first parameter is conventionally called argc. It is an integer, and indicates how many parameters were passed to the application. This parameter is very important. The reason is that you are about to access memory through an array, and this parameter gives the limit of your access. If you access memory beyond this limit you will have problems: at best you will be accessing uninitialized memory, but at worst you could cause an access violation.
It is important that, whenever you access memory, you understand the amount of memory you are accessing and keep within its limits.
The second parameter is usually called argv and is an array of pointers to C strings in memory. You will learn more about arrays and pointers in Chapter 4, Working With Memory, Arrays, and Pointers, and about strings in Chapter 9, Using Strings, so we will not give a detailed discussion here. The square brackets ([]) indicate that the parameter is an array, and the type of each member of the array is given by the char *. The * means that each item is a pointer to memory. Normally, this would be interpreted as a pointer to a single item of the type given, but strings are different: the char * means that in the memory the pointer points to there will be zero or more characters followed by the NUL character (). The length of the string is the count of characters until the NUL character.
The third line shown here prints to the console the number of strings passed to the application. In this example, rather than using the newline escape character (n) to add a newline, we use the stream std::endl. There are several manipulators you can use, which will be discussed in Chapter 6, Classes. The std::endl manipulator will put the newline character into the output stream, and then it will flush the stream. This line shows that C++ allows you to chain the use of the << put operator into a stream. The line also shows you that the << put operator is overloaded, that is, there are different versions of the operator for different parameter types (in this case, three: one that takes an integer, used for argv, one that takes a string parameter, and another that takes manipulator as a parameter), but the syntax for calling these operators is exactly the same.
Finally, there is a code block to print out every string in the argv array, reproduced here:
for (int i = 0; i < argc; ++i)
{
std::cout << argv[i] << std::endl;
}
The for statement means that the code block will be called until the variable i is less than the value of argc, and after each successful iteration of the loop, the variable i is incremented (using the prefix increment operator ++). The items in the array are accessed through the square bracket syntax ([]). The value passed is an index into the array.
Notice that the variable i has a starting value of 0, so the first item accessed is argv[0], and since the for loop finishes when the variable i has a value of argc, it means that the last item in the array accessed is argv[argc-1]. This is a typical usage of arrays: the first index is zero and, if there are n items in the array, the last item has an index of n-1.
Compile and run this code as you have done before, with no parameters:
C:\Beginning_C++\Chapter_01>simple
there are 1 parameters
simple
Notice that, although you did not give a parameter, the program thinks there is one: the name of the program executable. In fact, this is not just the name, it is the command used to invoke the executable. In this case, you typed the simple command (without the extension) and got the value of the file simple as a parameter printed on the console. Try this again, but this time invoke the program with its full name, simple.exe. Now you will find the first parameter is simple.exe.
Try calling the code with some actual parameters. Type the simple test parameters command in the command line:
C:\Beginning_C++\Chapter_01>simple test parameters
there are 3 parameters
simple
test parameters
This time the program says that there are three parameters, and it has delimited them using the space character. If you want to use a space within a single parameter, you should put the entire string in double quotes:
C:\Beginning_C++\Chapter_01>simple ″test parameters″
there are 2 parameters
simple
test parameters
Bear in mind that argv is an array of string pointers, so if you want to pass in a numeric type from the command line and you want to use it as a number in your program, you will have to convert from its string representation accessed through argv.