Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in c++ main function is the entry point to program how i can change it to an other function?

Tags:

c++

c

main

I was asked an interview question to change the entry point of a C or C++ program from main() to any other function. How is it possible?

like image 691
Badr Avatar asked Oct 20 '10 04:10

Badr


People also ask

What is entry point function in C?

What Is an Entry-Point Function? An entry point is a location in code where a transfer of program control (execution) occurs. The main function ( main() ) is the entry point to a C/C++ program and is called when the application starts executing.

Why main function is entry point?

Main function looks like entry point for application programmers (the application's entry point or main entry point). System programming reveals more information about the program, and specifies the entry point elsewhere (in initialization procedure, or in reset interrupt vector for standalone programs).

Can we change the name of main function in C?

You can't rename functions,variables, classes,..... in any programming language. In c, a name presents a pointers to location in the memory and that is static.In addition, even if you was able to change it's name, you will have then to change it's calling in the source code.

Which is the correct entry point of C or C Plus Plus program?

1. Using GCC _start function. As per C/C++ standard, main() is the starting point of any program in a hosted environment where a program uses the facilities of an operating system.


2 Answers

In standard C (and, I believe, C++ as well), you can't, at least not for a hosted environment (but see below). The standard specifies that the starting point for the C code is main. The standard (c99) doesn't leave much scope for argument:

5.1.2.2.1 Program startup: (1) The function called at program startup is named main.

That's it. It then waffles on a bit about parameters and return values but there's really no leeway there for changing the name.

That's for a hosted environment. The standard also allows for a freestanding environment (i.e., no OS, for things like embedded systems). For a freestanding environment:

In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. Any library facilities available to a freestanding program, other than the minimal set required by clause 4, are implementation-defined.

You can use "trickery" in C implementations so that you can make it look like main isn't the entry point. This is in fact what early Windows compliers did to mark WinMain as the start point.


First way: a linker may include some pre-main startup code in a file like start.o and it is this piece of code which runs to set up the C environment then call main. There's nothing to stop you replacing that with something that calls bob instead.


Second way: some linkers provide that very option with a command-line switch so that you can change it without recompiling the startup code.


Third way: you can link with this piece of code:

int main (int c, char *v[]) { return bob (c, v); } 

and then your entry point for your code is seemingly bob rather than main.


However, all this, while of possibly academic interest, doesn't change the fact that I can't think of one single solitary situation in my many decades of cutting code, where this would be either necessary or desirable.

I would be asking the interviewer: why would you want to do this?

like image 162
paxdiablo Avatar answered Sep 24 '22 20:09

paxdiablo


From C++ standard docs 3.6.1 Main Function,

A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function.

So, it does depend on your compiler/linker...

like image 21
liaK Avatar answered Sep 22 '22 20:09

liaK