What is the calling convention for the main function in C?
Asked Answered
S

2

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)?

Sublet answered 15/8, 2017 at 16:50 Comment(2)
The C language doesn't define any calling convention. You might want to add some more relevant tags and some more information in your questionEnder
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.Bush
H
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.

Hostelry answered 15/8, 2017 at 16:57 Comment(0)
R
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

Ripley answered 15/8, 2017 at 16:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.