Live data from Hacker News

What happens before main() is executed in C and why is it important?

mymicrocontroller.com

101–104 of 104 posts

Re: What happens before main() is executed in C and why is it important?

#101
post #99
post #84

Earlier quoted context omitted.

A couple of years ago there was a conference presentation (FOSDEM?) about what a program does before reaching main(). The presenter (a Brit IIRC) went pretty fast, did not go into too much detail and it took him 30-40 minutes. I think the study was done on a BSD. Maybe somebody can provide a link, the video is online, but Google resists to help me...

That sounds like Brooks Davis' talk that he has given at a few conferences: https://www.youtube.com/watch?v=yWCMy5EiNkQ https://people.freebsd.org/~brooks/talks/eurobsdcon2016-hell...

Exactly! The name of the speaker helps to find what I was looking for https://mirrors.dotsrc.org/fosdem/2017/Janson/hello_world.vp...

Re: What happens before main() is executed in C and why is it important?

#102

To me one curious thing about the main() function without arguments is, these arguments are still pushed onto the stack. You can find them by dereferencing a pointer of another stack variable and playing around with the offset. // Just tested with mingw-gcc on Win8.1 // Maybe you need to play around with the offset, +28 int main() { int i; // Another stack variable printf("%d\n", (long long)&i + 28); // &argv printf(…

Something surprising about C is that an empty argument list doesn't mean "no arguments", it means "unspecified arguments". https://stackoverflow.com/questions/693788/is-it-better-to-u... Though that is unlikely to be related to the arguments passed to main.

The C standard says arguments to main() should be defined as:

  int main(void)
or

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

  (or equivalent, eg. int can be replaced by a typedef name 
  defined as int, or the type of argv can be written as
  char ** argv).
From http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf, Section 5.1.2.2.1: Program startup)

EDIT: apologies for the atrocious formatting, but it wasn't clear to me how to put two asterisks in a sequence here on HN without preformatted text.

Re: What happens before main() is executed in C and why is it important?

#103

Earlier quoted context omitted.

Something surprising about C is that an empty argument list doesn't mean "no arguments", it means "unspecified arguments". https://stackoverflow.com/questions/693788/is-it-better-to-u... Though that is unlikely to be related to the arguments passed to main.

The C standard says arguments to main() should be defined as: int main(void) or int main(int argc, char *argv[]) (or equivalent, eg. int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv). From http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf , Section 5.1.2.2.1: Program startup) EDIT: apologies for the atrocious formatting, but it wasn't clear to me how to…

It actually has a whole other half to the sentence, following the semi-colon.

* http://jdebp.eu./FGA/legality-of-void-main.html

Re: What happens before main() is executed in C and why is it important?

#104
post #103

Earlier quoted context omitted.

The C standard says arguments to main() should be defined as: int main(void) or int main(int argc, char *argv[]) (or equivalent, eg. int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv). From http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf , Section 5.1.2.2.1: Program startup) EDIT: apologies for the atrocious formatting, but it wasn't clear to me how to…

It actually has a whole other half to the sentence, following the semi-colon. * http://jdebp.eu./FGA/legality-of-void-main.html

I took a detour at the footnote reference, and never came back. Interesting to note, thank you.
Post reply on HN