Da farko, a matsayina na ฦwararren mai haษakawa da ke da zurfin ilimi a cikin shirye-shiryen C, an yi mini tambaya akai-akai game da ko zai yiwu a rubuta shirin a cikin C ba tare da amfani da babban aikin ba. Amsar ita ce, eh, yana yiwuwa kuma zan yi bayanin yadda wannan ke aiki. Wasu masu haษawa kamar GCC, suna ba da damar wannan, kuma yana da ban sha'awa a cikin sassauฦa da yuwuwar shirye-shiryen C. Koyaya, ana ษaukar wannan motsi ba daidai ba ne. To me yasa wannan sha'awar ta taso? Domin a al'adance, an ce aiwatar da shirin C yana farawa daga babban aikin. Wannan gaskiya ne kuma yana aiki daidai ga mafi yawan ma'auni.
Aikin _start()
Mafarin farawa na ainihi a yawancin shirye-shiryen C shine ainihin aiki da ake kira _start(). Yawancin lokaci, wannan aikin bootstrap ne wanda ke fara samar da albarkatun da ake buฦata kuma a ฦarshe ya kira babban (). An shigar da aikin _start() a cikin shirin C ta mai tarawa, dabam da lambar tushen ku. Saboda haka, ya riga ya wanzu kuma yana yin aikinsa a asirce a bayan fage ba tare da yawancin masu haษakawa suna buฦatar saninsa ba.
Amfani da _start () a wurin babban()
Sirrin ra'ayi na gudanar da shirin C ba tare da babban() yana tare da kawo wannan ba _start () aiki a cikin fage kuma yadda ya kamata amfani da shi a madadin main(). Wannan ita ce dabarar da masu tara GCC ke ba mu damar yi. Ga yadda:
void _start() { // Add your code here _exit(0); }
Kula da amfani da aikin _exit() a ฦarshen. Wannan yana da mahimmanci saboda idan shirin bai kira _exit() ba, shirin zai fadi.
Fahimtar Gudun Shirin
Wannan shi ne yadda shirin zai yi aiki: A daidaitattun shirye-shiryen C, aikin _start () wanda mai tarawa ya samar, zai kira babban () aikin. Koyaya, a wannan yanayin, mun maye gurbin babban () aikin da namu _start (), kuma yana aiwatarwa kai tsaye maimakon kiran main(). Ayyukan _exit() kira ne na tsarin da zai ฦare shirin mu daidai.
Bayanan kula akan Dogaran Laburare
- Duk da yake yana da ban sha'awa don ฦididdige shirin C ba tare da babban() ba, akwai ฦดan koma baya. Wannan aikin yana gabatar da adadi mai kyau dogaro da ษakin karatu cikin code din ku. Misali, don amfani da aikin _exit(), ษakin karatu na unistd.h ya zama dole.
- Wannan bazai zama matsala da farko ba, amma idan kuna tsammanin lambar ku ta gudana akan tsari da mahalli daban-daban, ana ba da shawarar ku tsaya tare da babban aikin () saboda yana kawo babban matakin ษaukar nauyi.
A ฦarshe, mun ga yadda zai yiwu a ฦirฦira wani shiri na musamman na C wanda ke gudana ba tare da babban aikin () ba. Wannan shaida ce ga sassauฦar harshe na C. Yana da mahimmanci, duk da haka, a lura cewa wannan al'ada ba ta dace ba, don haka amfani da hankali.