An warware: za mu iya rubuta shirin ba tare da main a c

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.

Shafi posts:

Leave a Comment