main Function

This page was translated by a robot.

The mainfunction designates the start of each C program. If the mainfunction is exited, the program is terminated with the specified return value.




Hello World!
The Debugger has exited with status 0.
#include <stdio.h>

int main(){
  printf("Hello World!");
  return 0;
}

The system transfers a so-called argument vector to each C program , which maincan be addressed with the extended declaration:

int main(int argc, char** argv);

Details

The main function can only be defined once per program and it must be in the global scope. The file in which it is defined is referred to as the primary source file . Traditionally, the main function is located at the end of the primary source file. This is common because all of the variables and functions that the main function calls must have been declared somewhere. By defining the main function at the end of the file, the required symbols are automatically located somewhere before the main function, and there is no need to write prototypes (which can be annoying, especially for small programs).

mainIn the different C dialects and with different programmers, the function can assume a wide variety of forms, which are basically all equivalent . The following is the most common information:

void main();
void main(void);
void main(int argc, char** argv);
void main(int argc, char* argv[]);
int  main();
int  main(void);
int  main(int argc, char** argv);
int  main(int argc, char* argv[]);

int  main(int argc, char** argv, char** env);

The return value of the function can be intor void, whereby inta value can only be returned with the return value. With void, the compiler assumes the default return value 0. ISO-C requires the return value int.

The system always passes mainan argument vector to the function. A parameterless (or voiddeclared with parameter) mainfunction ignores this vector. However, if this vector is required, the main-function is always declared with the two parameters argcand , as is generally known argv. The parameter argcdesignates the argument count , the number of arguments in the vector. The parameter argvis the argument vector , an array of strings ( char*).

The following example explains how to use these parameters:

./Program yeah! -s hello world



Number of arguments: 5


Argument 0: ./Program
Argument 1: yeah!
Argument 2: -s
Argument 3: hello
Argument 4: world


#include <stdio.h>

int main(int argc, char** argv){
  printf("Number of arguments: %d\n", argc);
  int i;
  for(i = 0; i < argc; ++i){
    printf("Argument %d: %s\n", i, argv[i]);
  }
  return 0;
}

The argument with index 0always designates the name of the program as it was called (this can be an absolute or relative path specification). This argument is standardized, argcso it is always at least 1. All remaining arguments correspond to the strings that were specified when the program was called, separated by spaces. There is always an additional, last entry in the array, which is set to NULLby default. This last entry is also known as the Sentinel .

The last line of the above main declarations shows a rather unknown property of the main function. The optional third parameter contains a list of environment variables, which are passed from the system to the program. This parameter is usually envnamed (environment variables). There is no default counter variable, but just like the argument list, this array is terminated by default with a sentinel NULLset to . The following code explains how to use this parameter, although the output can be different for each user:

__CF_USER_TEXT_ENCODING=0x1F5:0:3
NSUnbufferedIO=YES
SHELL=/bin/bash
HOME=/usr
DYLD_NO_FIX_PREBINDING=YES
COMMAND_MODE=unix2003
DYLD_NEW_LOCAL_SHARED_REGIONS=YES
PATH=/usr/bin:/bin:/usr/sbin:/sbin
DISPLAY=/tmp/launch-AYgTi7/:0
USERBREAK=1
...
#include <stdio.h>

int main(int argc, char** argv, char** env){
  int i = 0;
  while(1){
    if(!env[i]){break;}
    printf("%s\n", env[i]);
    i++;
  }
  return 0;
}

Return Values

The mainfunction is also a completely normal function and, depending on the declaration, can be completed with the return of a value. Usually, a program is terminated by the return value 0, whereas any non-zero value indicates that the program ran erroneously. However, the return value is not standardized and can be freely selected by the programmer. This value is normally not important, but can be additional information for the calling program when working with multiple processes or threads.

If the program is executed in a terminal, the terminal will respond with a message like the following after the program has finished:



Program has exited with status 10.
int main(){
  return 10;
}

Caution: The declared type of the mainfunction is by default int, but the parent program (e.g. a terminal) does not necessarily have to interpret this value as such. For example, tests on the author's system have shown that the return type unsigned charis interpreted as depending on the application.