0

I just want to learn the basics thoroughly and what some simple codes refer to.

I was able to find a short description at https://www.dummies.com/programming/c/looking-at-the-c-language/ but I dont think I fully understand it with the help of just that.

  • It's the starting point of your program - where the operating system knows to go to start executing your code. – Michael Dorgan Jul 10 '19 at 23:25
  • Is there any reason that it is "main" and not something like "start" or "beginning" instead, Im guessing theres not? – script kiddie Jul 10 '19 at 23:28
  • @scriptkiddie its because `crt0` calls `main` see: https://www.embecosm.com/appnotes/ean9/html/ch05s02.html – bigwillydos Jul 10 '19 at 23:33
  • As to why it is called main take a look at this https://stackoverflow.com/questions/1688338/why-the-name-main-for-function-main – Jerry Jeremiah Jul 10 '19 at 23:41
  • main is the first function executed that i s written by you - it isn't the first function executed because there is code that is run before your program starts that, for example. reads the command line and puts it into the argv array. – Jerry Jeremiah Jul 10 '19 at 23:45
  • It's called `main` for the more or less the same reason you're called script kiddie. It's not called `start` or `beginning` for more or less the same reason you're not called Tiggles or Sean the Avenger. – Steve Summit Jul 11 '19 at 00:55
  • @scriptkiddie: The entry point needs to be called *something*. `main` was as good a name as any, and it makes sense, as it’s the “main” part of your program. – John Bode Jul 11 '19 at 04:01

1 Answers1

1

It's the starting point for your program. Per 5.1.2.2.1 Program startup of the C standard:

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

     int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

     int main(int argc, char *argv[]) { /* ... */ }

or equivalent; or in some other implementation-defined manner.

If they are declared, the parameters to the main function shall obey the following constraints:

  • The value of argc shall be nonnegative.
  • argv[argc] shall be a null pointer.
  • If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.
  • If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.
  • The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.
Andrew Henle
  • 32,625
  • 3
  • 24
  • 56