Is it possible to write c program without main




















Alok Save Alok Save k 44 44 gold badges silver badges bronze badges. Are you sure? I mean everyone writing an OS kernel or embedded programs will have to do without a main function and I'd hope that such a thing wouldn't result in undefined behavior.

It seems weird to me that first you say "No you cannot. Johannes Schaub - litb: Ah, I will add the exception of freestanding environments. Voo: No freestanding environments like embedded programs or OS kernels can omit the main , check the quote from the standard. Lightness Races in Orbit k 70 70 gold badges silver badges bronze badges. Cheers and hth. I'd planned to just let this answer B, with mistake for all 2 C. OK, just watched Star Wars movie. It was complemented and corrected by e.

Als' answer and Johannes Schaub's answer. Now, in case a causal later reader doesn't see it: a frestanding environment , e. I'm not the anonymous downvoter - but I suspect its a vote against this being the accepted answer, when it is not accurate. Its a really poor interview question - but this answer is misleading as it is only true for the admittedly more common hosted environment.

Sample program without a visible main function. This code is not without main. Johannes Schaub - litb Johannes Schaub - litb k gold badges silver badges bronze badges. To include a C module And I'm also switching to x here.

DigitalRoss DigitalRoss k 23 23 gold badges silver badges bronze badges. Yes it possible to compile with out main but you cannot pass the linking phase though. Mahesh Mahesh 33k 17 17 gold badges 82 82 silver badges bronze badges. Why would MainSub::MainSub be executed?

You don't construct a MainSub object anywhere? Dan, yes I do. Right below the struct's definition, and above int main. Oh right, I thought that would be just a declaration. If it were a primitive i. Dan, if it were an int , it would have been initialized to 0. I never knew that Joohae Kim Joohae Kim 41 4 4 bronze badges.

For example, you can make a simple helloworld using write function on descriptor 1. Clifford Clifford John Gummadi John Gummadi 11 1 1 bronze badge. Good fun JorenHeit JorenHeit 3, 2 2 gold badges 18 18 silver badges 26 26 bronze badges. CreatorGhost CreatorGhost 5 5 bronze badges. Maybe it's possible to compile a. Micromega Micromega Did they mean: Write a program with no main function. Then generally speaking no. But there are ways to cheat. You can use the pre-processor to hide main in plain sight.

Most compiler allow you to specify the entry point into your code. Martin York Martin York k 78 78 gold badges silver badges bronze badges. Anshuman Singh Anshuman Singh 1, 1 1 gold badge 13 13 silver badges 21 21 bronze badges. But it uses main indirectly. In the 2nd line of the program- define decode s,t,u,m,p,e,d m s u t What is the preprocessor doing here.

Community Bot 1 1 1 silver badge. Saraswati Katakdhond Saraswati Katakdhond 41 4 4 bronze badges. The time stamps tell me that was 5 hrs before you posted this answer. Als : thanks for making me understand the reason. By the way, that wasn't my answer. I was just trying to tell if this trick I want to reference that one is valid, then any much easier trick should work like my example. And I forgot to read the comments posted below the answers.

You will need to read the SO guidelines. In a nutshell post which answers the Q should be posted as Answers. The following function uses the macro operator to hide the main method. However, the code still does make a call to the main function behind the scenes, but just not in plain sight. Output: Hello World. Rate this post. Average rating 4. Vote count: No votes so far! Be the first to rate this post. Primary Primary. Skip to content. Skip to content. Change Language.

Related Articles. Table of Contents. Save Article. Improve Article. Like Article. Output: Geeksforgeeks Compilation using command : gcc filename.

Previous Amazon Interview Set



0コメント

  • 1000 / 1000