5

Each function in C must have a calling convention, but what is the calling convention for the main function (I think it is the cdecl calling convention but I am not sure)?

johnny92
  • 85
  • 1
  • 1
  • 6
  • 5
    The C language doesn't define any calling convention. You might want to add some more relevant tags and some more information in your question – Jabberwocky Aug 15 '17 at 16:51
  • `main` has the same calling convention as any other function; `_start` (a typical entry point in ELF), on the other hand, _is_ cdecl and must handle converting to the native calling convention for main (among other things) ... don't know why `_start` doesn't use the native calling convention... probably because in Linux, the binfmt_elf source is in the ./fs (file system) directory instead of ./arch and 32 bit x86 used cdecl, so it was easy to be lazy and force every non-cdecl platform to require some assembly or compiler specific intrinsics in their libc. – technosaurus Aug 15 '17 at 18:24

2 Answers2

4

That depends on the architecture and platform. A lot of x86 C runtime specifications require that main be cdecl, but it's by no means guaranteed.

The bottom line is you're not going to find this information in the C standard because the language is not tied to any one architecture. You might have more luck reading the documentation for the particular compiler(s) you're interested in.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
2

C language does not define calling convention but the processor architecture and development platform does. For X86 calling convention please check wiki https://en.wikipedia.org/wiki/X86_calling_conventions

Also, see ARM calling convention at below link http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042f/IHI0042F_aapcs.pdf

More on calling convention see below wiki link https://en.wikipedia.org/wiki/Calling_convention

Also, check discussion about MIPS calling convention at GCC MIPS-32 Calling Conventions / Stack Frame Definition

MCG
  • 1,011
  • 1
  • 10
  • 21