![]() |
C - Entwicklung Programm - Flusskontrolle: Start & Stop Homepage von PS-Trainer - C-Entwicklung - Flusskontrolle - an PS-Trainer |
|
| Program Flow Control | |
| Startup and Termination | C++ Program Startup and Termination |
| main | Every C program has a primary (main) function that must be named main. |
| Command line arguments | Arguments main can receive from the command line. |
| _pgmptr, _wpgmptr | Pointer to the full path of the executable file. |
| abort | Aborts the current process and returns an error code. |
| exit | The exit function terminates a C++ program. |
| atexit | Processes the specified function at exit. |
| terminate | The function calls the current terminate handler |
| C++ Program Startup and Termination Program startup and termination is facilitated by using two functions: main and exit. Other startup and termination code may be executed. A C++ program performs the same operations as does a C program at program startup and at program termination, plus a few more outlined here. Before the target environment calls the function main, and after it stores any constant initial values you specify in all objects that have static duration, the program executes any remaining constructors for such static objects. The order of execution is not specified between translation units, but you can nevertheless assume that four iostreams objects are properly initialized for use by these static constructors. These control several text streams: cin -- for standard input cout -- for standard output cerr -- for unbuffered standard error output clog -- for buffered standard error output During program termination, you can also use these objects within the destructors called for static objects. As with C, returning from main or calling exit calls all functions registered with atexit in reverse order of registry. An exception thrown from such a registered function calls terminate(). Keywords: _pgmptr, main, abort, exit, terminate |
| The main
Function and Program Execution Every C program has a primary (main) function that must be named main. If your code adheres to the Unicode programming model, you can use the wide-character version of main, wmain. The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program. A program usually stops executing at the end of main, although it can terminate at other points in the program for a variety of reasons. At times, perhaps when a certain error is detected, you may want to force the termination of a program. To do so, use the exit function. See the Run-Time Library Reference for information on and an example using the exit function. Functions within the source program perform one or more specific tasks. The main function can call these functions to perform their respective tasks. When main calls another function, it passes execution control to the function, so that execution begins at the first statement in the function. A function returns control to main when a return statement is executed or when the end of the function is reached. You can declare any function, including main, to have parameters. You can declare formal parameters to main so that it can receive arguments from the command line using this format: main( int argc, char *argv[ ], char *envp[ ] ) When you want to pass information to the main function, the parameters are traditionally named argc and argv, although the C compiler does not require these names. The types for argc and argv are defined by the C language. Traditionally, if a third parameter is passed to main, that parameter is named envp. The type for the envp parameter is mandated by ANSI, but the name is not. See wmain for a description of the wide-character version of main. |
| Command line arguments
You can declare formal parameters to main so that it can receive arguments from the command line using this format: main( int argc, char *argv[ ], char *envp[ ] ) |
||||||||||||||||||||||||
| Arguments to main ANSI 2.1.2.2.1 The semantics of the arguments to main (command line arguments) In Microsoft C, the function called at program startup is called main. There is no prototype declared for main, and it can be defined with zero, two, or three parameters: int main( void ) int main( int argc, char *argv[] ) int main(
int argc, char *argv[],
char *envp[] )
The third line above, where main accepts three parameters, is a Microsoft extension to the ANSI C standard. The third parameter, envp, is an array of pointers to environment variables. The envp array is terminated by a null pointer. The variable argc never holds a negative value. The array of strings ends with argv[argc], which contains a null pointer. All elements of the argv array are pointers to strings. A program invoked with no command-line arguments will receive a value of one for argc, as the name of the executable file is placed in argv[0]. (In MS-DOS versions prior to 3.0, the executable-file name is not available. The letter "C" is placed in argv[0].) Strings pointed to by argv[1] through argv[argc 1] represent program parameters. The parameters argc and argv are modifiable and retain their last-stored values between program startup and program termination. |
||||||||||||||||||||||||
| Argument Description The argc parameter in the main and wmain functions is an integer specifying how many arguments are passed to the program from the command line. Since the program name is considered an argument, the value of argc is at least one. The variable argc never holds a negative value. The argv parameter is an array of pointers to null-terminated strings representing the program arguments. Each element of the array points to a string representation of an argument passed to main (or wmain). The array of strings ends with argv[argc], which contains a null pointer. The argv parameter can be declared either as an array of pointers to type char (char *argv[]) or as a pointer to pointers to type char (char **argv). For wmain, the argv parameter can be declared either as an array of pointers to type wchar_t (wchar_t *argv[]) or as a pointer to pointers to type wchar_t (wchar_t **argv). The first string (argv[0]) is the program name. The last pointer (argv[argc]) is NULL. (See getenv in the Run-Time Library Reference for an alternative method for getting environment variable information.) The envp parameter is a pointer to an array of null-terminated strings that represent the values set in the user's environment variables. The envp parameter can be declared as an array of pointers to char (char *envp[]) or as a pointer to pointers to char (char **envp). In a wmain function, the envp parameter can be declared as an array of pointers to wchar_t (wchar_t *envp[]) or as a pointer to pointers to wchar_t (wchar_t **envp). The end of the array is indicated by a NULL *pointer. Note that the environment block passed to main or wmain is a "frozen" copy of the current environment. If you subsequently change the environment via a call to _putenv or _wputenv, the current environment (as returned by getenv/_wgetenv and the _environ or _wenviron variables) will change, but the block pointed to by envp will not change. |
||||||||||||||||||||||||
| Parsing C Command-Line Arguments Microsoft Specific ! Microsoft C startup code uses the following rules when interpreting arguments given on the operating system command line: This list illustrates the rules above by showing the interpreted result passed to argv for several examples of command-line arguments. The output listed in the second, third, and fourth columns is from the ARGS.C program that follows the list.
|
||||||||||||||||||||||||
| /* ARGS.C illustrates the following
variables used for accessing * command-line arguments and environment variables: * argc argv envp */ #include <stdio.h> void main( int argc, /* Number
of strings in array argv */ /* Display each command-line
argument. */ /* Display each environment
variable. */ Environment
variables: | ||||||||||||||||||||||||
| Expanding Wildcard Arguments Microsoft Specific ! When running a C program, you can use either of the two wildcards the question mark (?) and the asterisk (*) to specify filename and path arguments on the command line. Command-line arguments are handled by a routine called _setargv (or _wsetargv in the wide-character environment), which by default does not expand wildcards into separate strings in the argv string array. You can replace the normal _setargv routine with a more powerful version of _setargv that does handle wildcards by linking with the SETARGV.OBJ file. If your program uses a wmain function, link with WSETARGV.OBJ. To link with SETARGV.OBJ or WSETARGV.OBJ, use the /link option. For example: cl typeit.c /link setargv.obj The wildcards are expanded in the same manner as operating system commands. (See your operating system user's guide if you are unfamiliar with wildcards.) Enclosing an argument in double quotation marks (" ") suppresses the wildcard expansion. Within quoted arguments, you can represent quotation marks literally by preceding the double-quotation-mark character with a backslash (\). If no matches are found for the wildcard argument, the argument is passed literally. |
| _pgmptr, _wpgmptr
When a program is run from the command interpreter (CMD.EXE), _pgmptr is automatically initialized to the full path of the executable file. For example, if HELLO.EXE is in C:\BIN and C:\BIN is in the path, _pgmptr is set to C:\BIN\HELLO.EXE when you execute C> hello When a program is not run from the command line, _pgmptr may be initialized to the program name (the file's base name without the extension), or to a filename, a relative path, or a full path. _wpgmptr is the wide-character counterpart of _pgmptr for use with programs that use wmain. _pgmptr and _wpgmptr are declared in STDLIB.H as extern char *_pgmptr; extern wchar_t *_pgmptr; |
| The following program demonstrates the use of _pgmptr. /* * PGMPTR.C: The following program demonstrates the use of _pgmptr. */ #include <stdio.h> |
| abort Aborts the current process and returns an error code. void abort( void );
Remarks The abort routine prints the message "abnormal program termination" and then calls raise(SIGABRT). The action taken in response to the SIGABRT signal depends on what action has been defined for that signal in a prior call to the signal function. The default SIGABRT action is for the calling process to terminate with exit code 3, returning control to the calling process or operating system. abort does not flush stream buffers or do atexit/_onexit processing. abort determines the destination of the message based on the type of application that called the routine. For more information, see Using C Run-Time Library Debugging Support. Subject: Process and Environment Control Routines Keywords: See also _exec Function Overview, exit, raise, signal, _spawn Function Overview, _DEBUG |
||||||
| Return Value abort does not return control to the calling process. By default, it terminates the current process and returns an exit code of 3. |
||||||
| Example /* ABORT.C: This program tries to open a * file and aborts if the attempt fails. */ #include <stdio.h> void main( void ) |
| exit Function The exit function, declared in the standard include file STDLIB.H, terminates a C++ program. The value supplied as an argument to exit is returned to the operating system as the program's return code or exit code. By convention, a return code of zero means that the program completed successfully. Note You can use the constants EXIT_FAILURE and EXIT_SUCCESS, defined in STDLIB.H, to indicate success or failure of your program. Issuing a return statement from the main function is equivalent to calling the exit function with the return value as its argument. Subject: Run-Time Library |
| Using exit or return When you call exit or execute a return statement from main, static objects are destroyed in the reverse order of their initialization. This example shows how such initialization and cleanup works: #include <stdio.h> class ShowData // Define a static object of
type ShowData. The output device int main() |
| atexit Processes the specified function at exit. int atexit( void ( __cdecl *func )( void ) );
Remarks The atexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to atexit create a register of functions that are executed in LIFO (last-in-first-out) order. The functions passed to atexit cannot take parameters. atexit and _onexit use the heap to hold the register of functions. Thus, the number of functions that can be registered is limited only by heap memory. Subject: Process and Environment Control Routines Keywords: See also abort, exit, _onexit |
||||||
| Return Value atexit returns 0 if successful, or a nonzero value if an error occurs. Parameter
|
||||||
| Example /* ATEXIT.C: This program pushes four functions onto * the stack of functions to be executed when atexit * is called. When the program exits, these programs * are executed on a "last in, first out" basis. */ #include <stdlib.h> void fn1( void ), fn2( void ), fn3( void ), fn4( void ); void main( void ) void fn1() void fn2() void fn3() void fn4() |
| terminate void terminate(); The function calls the current terminate handler. A function of type void () called when exception handling must be abandoned for any of several reasons. A terminate handler may not return to its caller. At program startup, the terminate handler is a function that calls abort(). |
| Aktuelle Daten dieser Seite | Letzte Änderung: |
| |