0% found this document useful (0 votes)
137 views3 pages

Why Do We Use Main in C or C Language - Quora

The main() function in C and C++ is used to mark the entry point of a program where execution begins. While main() may seem like the actual starting point, several things like initializing global objects occur before main() is called. The name main() is simply a convention that has been standardized for hosted environments that require a starting point, but other languages or environments may use different naming or mechanisms.

Uploaded by

api-325971962
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views3 pages

Why Do We Use Main in C or C Language - Quora

The main() function in C and C++ is used to mark the entry point of a program where execution begins. While main() may seem like the actual starting point, several things like initializing global objects occur before main() is called. The name main() is simply a convention that has been standardized for hosted environments that require a starting point, but other languages or environments may use different naming or mechanisms.

Uploaded by

api-325971962
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

8/6/2016

Whydoweusemain()inCorC++language?Quora

C(programminglanguage)
ProgrammingLanguages

C++(programminglanguage)

LearningtoProgram

ComputerProgramming

Whydoweusemain()inCorC++language?
AlsoisthereanyprograminCorC++whichcanberunsuccessfullywithoutmain()?

19Answers
ViktorT.Toth,beganhisprogrammingcareeronaCDC3300mainframeandanSR
52calculator
16.5kViewsViktorhas90+answersinComputerProgramming

WhatmakesyouthinkthattheHello,Worldprogramrunswithoutacalltothefunction
main()?
Thefunctionmain()is,infact,called.Butitisnotcalledexplicitlythecallisimplicit.
Whichmeansthatyoudonothavetowritethatcallintoyourprogram,itisdone
Searchforquestions,people,andtopics
automaticallybytheruntimeenvironment.
Inotherwords,whenyoucompileaCprogram,theactualcodegeneratedbythecompiler
beginswithapreamblethatsetsupmemory,configuresstandardinputandoutput,and
doesafewotherhousekeepingchores,followedbyacalltothemain()function,followed
bycleanupandterminationcode.

RelatedQuestions
WhywasC++chosenasthemainlanguagefor
backenddevelopmentwithinGoogle?Whyisitstill
themainlanguage?
Asof2016,isCthemainlanguagefornewWindows
kernelcodeorisC++nowused?Isassemblystill
activelyused?
Isclanguagecoachinguseful?
WhicharesomeStartupsinBangaloreworkingon
Systemsside/usingCasmainlanguage?
WhatarethemainareaswhereCisused?
WhydowestilluseCandC++languages?
WhichsoftwaregiantsuseC++astheirmain
SignIn
technology?
Whyweusevoidmain()intmain()inC?
WhatisClanguageusedfor?
WhataretheadvantagesofusingCoverC++?

Ifyouweretotrytocompileaprogramwithnomain()function,thelinkerwouldactually
complainaboutitsabsence,becauseitisreferencedintheruntimeenvironment.
WrittenSep3,2015ViewUpvotes

RelatedQuestions
MoreAnswersBelow
WhywasC++chosenasthemainlanguageforbackenddevelopmentwithinGoogle?Why
isitstillthemainlanguage?
Asof2016,isCthemainlanguagefornewWindowskernelcodeorisC++nowused?Is
assemblystillactivelyused?
Isclanguagecoachinguseful?
WhicharesomeStartupsinBangaloreworkingonSystemsside/usingCasmainlanguage?
WhatarethemainareaswhereCisused?

TribhubanJha
4.4kViewsTribhubanhas30+answersinComputerProgramming

main()isauserdefinedfunctionfromwhereexecutionofC/C++programsstart!
Now,ifuaskwhyshoulditstartwithmain(),wellithadtostartsomewhereandDennis
Ritchiesirchoseittobehere.:)
Edit:
Havealookatthisforbetterunderstandingofinsight:)RaghavYadav'sanswertoWhat
aretheC/C++programsthatcanbewrittenwithoutmain()function?
UpdatedOct21,2014ViewUpvotes

JerryCoffin,Guywhowritessomecode.
2.4kViewsJerryhas180+answersinComputerProgramming

Althoughseveralhavehintedatit,thename`main`issimplyaconventionthat'sbeen
standardized.Inahostedenvironment(basically,whereyou'rerunningonanoperating
system)theCandC++standardsrequirethatyourcodecontainafunctionnamed
`main`,andthatitbewhereexecutionstarts(sortofglobalobjectsareinitializedbefore
mainstartstoexecute,soifyouprefer,youcanstartyourexecutionfromaconstructorof
aglobalobject,andleavemainemptybutthatwouldbecounterintuitiveenoughand
unusualenoughthatit'sgenerallyabadidea).

Onastandalonesystem(onewhereyourcoderunsdirectlyonthe"baremetal",without
anoperatingsystem),there'snorequirementaboutwhattheentrypoint(ifany)isnamed.
Insomecases,thereisnorealentrypointforexample,somesystemsjusthandle
hardwareinterrupts,sotheyhaveanumberofinterrupthandlers,andwhenthere'sno

https://www.quora.com/WhydoweusemaininCorC++language

1/3

8/6/2016

Whydoweusemain()inCorC++language?Quora

interrupt,theCPUjustsleeps.

Incaseanybodycareswherethename`main`camefrom,theearliestlanguageofwhich
I'mawarethatusedthatnameforaprogram'sentrypointwasPL/I(fromaround1964).
WrittenMay19,2015ViewUpvotes

AndrewWeimholt
7.9kViewsAndrewhas30+answersinComputerProgramming

Themainisnottherealentrypoint.
Keepinmindtherearealotofthingsthathappenbeforemainiscalled.InC++,your
globalconstructorsarecalledbeforemain.EveninstraightC,therearethingsthatneedto
bedonebeforeyoucanexecute"main".
Ifyou'reusinggcc,it'sthe_startfunction(InsideC)
Here'sabetterarticleonhowthelinuxloaderstartsanexecutable"ELF"file:Linuxx86
ProgramStartUp
Notethatthefirstarticleshowsyouhowtooverride_startanddowithouthavingamain.
Searchforquestions,people,andtopics
Youcanhavea_startfunctionwhichdoesallthenormalstuffandthencallsomeother
functionasyourpseudoentrypointinsteadofmain.

SignIn

WrittenSep4,2014ViewUpvotes

XiangyuHu,Doneisbetterthanperfect.
2.1kViews

Fromcompiler'sview,main()isnothingdifferentthanotherexportedfunction,except
thatitisTHEcallbackentryofexecutableformats.Compiler'saddinstructionslike"bl
main"inthe_start()(executableentry,differsbetweendifferentplatforms)function.
WrittenSep28,2014ViewUpvotes

RelatedQuestions
WhydowestilluseCandC++languages?
WhichsoftwaregiantsuseC++astheirmaintechnology?
Whyweusevoidmain()intmain()inC?
WhatisClanguageusedfor?
WhataretheadvantagesofusingCoverC++?
HowcanIlearnOOPSConcept,UML,ObjectOrientedProgramminglanguage(mainly
C++)?
C++(programminglanguage):Whydopeoplewhocomefromaphysicsbackgroundtendto
useC++?
WhatisthemainuseanddifferencebetweenforandwhileinC/C++languages?
IsthereagoodonlinecourseaboutalgorithmsthatuseC++astheirmainprogramming
language?
InwhichcasesshouldIusePython/Node.JS/Java/Rails/CC++asthemainbackend
language?
WhydoesUnityGameEngineuseC#asthemainscriptinglanguage?
What'sthereasontouseCasthemaindevelopmentlanguageinyourprojects?
Wheremain()functiondefinedinclanguage?
C++(programminglanguage):InC++,whydoyouusereferences?
WhatlanguagedoesArduinouse?C/C++orprocessing.

TopStories
Whatisthestrangestcombination
oftwofoodsyouveevereaten?

Whatisthesmartestthingyou
haveeverdoneinexams?

MeredithAvila,certainkindsofnerd

KevinMarkWray

3.1kViews

49.7kViews

https://www.quora.com/WhydoweusemaininCorC++language

Whydidthemediaseverely
criticizeBernieSandersfor
attackingHillaryClintonbuthas
largelyignoringtherhetoric
duringtheGOPconvention?

2/3

8/6/2016

Whydoweusemain()inCorC++language?Quora

WhenIwasakidIusedtomakesandwiches
madeoftwoofthis:Andthis:Ithadavery
ReadMore
satisfyingtasteandIgreatlyenjoyedeating

ItookaconversationalGermanclassin
college.Itwasat8am.Itwasbrutal.
Fortunately,thanksto5yearsofGermanin
middleandhighschool,Iwasokayasfaras
conversationalGermangoes.Iperpetually
showedupforeveryclass510minuteslate
andhadaratherterseargumentwiththe
professorinGermanaboutmylateness.One
studenttoldmeIwasgoingtofailforsureto
ReadMore
whichIsai...

ErnestW.Adams,GameDesign
Consultant,Author,andProfessor
3.3kViewsMostViewedWriterinPolitics
oftheUnitedStatesofAmericawith1290+
answers

Theconservativemediaweresaying,"See!
See!TheDemocratsaredividedtoo!Anda
lotofthemthinkHillaryisscumtoo!"The
liberalmediaweresaying,"C'mon,Bernie,
ReadMore
keepitimpersonalanddon'tgivethe
Republicansammunition."Today,the

Sitemap # A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
AboutCareersPrivacyTermsContact

https://www.quora.com/WhydoweusemaininCorC++language

3/3

You might also like