Earlier, you found that themain 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 thesimple command on the command line. In Windows, the error code is obtained through the pseudo environment variableERRORLEVEL, so obtain this value through theECHO 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 themain 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 themain function. Replace themain 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 themain function to take parameters from the command line, the convention is that it has these two parameters.
The first parameter is conventionally calledargc. 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 calledargv and is an array of pointers to C strings in memory. You will learn more about arrays and pointers inChapter 4,Working With Memory, Arrays, and Pointers, and about strings inChapter 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 thechar *. 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: thechar * means that in the memory the pointer points to there will be zero or more characters followed by theNUL character (). The length of the string is the count of characters until theNUL 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 streamstd::endl. There are several manipulators you can use, which will be discussed in Chapter 6,Classes. Thestd::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 forargv, 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 theargv array, reproduced here:
for (int i = 0; i < argc; ++i)
{
std::cout << argv[i] << std::endl;
}
Thefor statement means that the code block will be called until the variablei is less than the value ofargc, and after each successful iteration of the loop, the variablei is incremented (using the prefix increment operator++). The items in the array are accessed through the square bracket syntax ([]). The value passed is anindex into the array.
Notice that the variablei has a starting value of0, so the first item accessed isargv[0], and since thefor loop finishes when the variablei has a value ofargc, it means that the last item in the array accessed isargv[argc-1]. This is a typical usage of arrays: the first index is zero and, if there aren items in the array, the last item has an index ofn-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 thesimple command (without the extension) and got the value of the filesimple 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 issimple.exe.
Try calling the code with some actual parameters. Type thesimple test parameters command in the command line:
C:\Beginning_C++\Chapter_01>simple test parameters
there are 3 parameters
simple
testparameters
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 thatargv 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 throughargv.