Many think that when the main() function call inside the main(), recursion goes on infinite times. But it is not true as such. It is always important to know how function calling and recursion works. The function calls work with the help of a stack by PUSHing all the functions to a stack and POPing the functions from the stack when they are executed. The following figure gives the complete picture of this working. When a function "func1()" is called from within the "main()" method, the "func1()" is PUSHed onto the stack. Similar is the case with the function "func2()". When the function "func2()" gets completed, it is POPed from the stack. The function calls take place until the stack overflow occurs, that is until the stack is completely filled with the functions. This implies that the recursion without a suitable condition takes place not infinite times but stack size times.
This particular experiment when conducted, compiles successfully but it suffers from a run time error.
Lets have a look at the functionality of this entire concept and thus decide the output of the program. We also experiment on the number of times the function is called. The entire simulation is with the help of a simple program as given below:
#include "stdio.h"
int i=0;
int main()
{
i++;
printf(" %d\n",i);
main(); /* Call main() inside main() */
return 0;
}
Now,in the above program the value of "i" signifies the number of times the function is called.
First, I considered running this program on Turbo C on Windows platform, where the size of a variable of type "int" is 2 bytes. When I ran this program, the function got called around 15000 times and then the compiler crashed with the error message in the image shown below:
The next time I tried the same program on GNU C compiler on Ubuntu 10.04, where the size of a variable of type "int" is 4 bytes. Now when the program is compiled, it returned successful compilation.
But when it is run, a runtime error was issued as "Segmentation fault". The error message in the dialog box for Turbo C also indicated the same thing.
Now what is this segmentation fault? It is an error that occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed. It occurred in this case due to the stack overflow. So, main() called inside main() gives segmentation fault.
The number of times the function call took place varies continuously between two executions of the program. In Turbo C, the number of times the function call took place is around 16000 while in GNU C Compiler, this figure is around 327000.
great
ReplyDeleteawesome reply..
ReplyDeletethanks
But why does the below program when run gives 0 0 0 0 as the output ?
ReplyDeleteint main()
{
static int i=5;
if(--i){
main();
printf("%d ",i);
}
}
pre decrement of i takes place.
Deletesince it is given in if block it executes until i>0
since this is lower bound problem it executes only 4 time for i = 5.
thanks man
ReplyDeleteawesome......
Tq
ReplyDeleteThank you for providing me with this information. I really appreciate it. The site will be great. Visit the Everything you need to know to improve your WPM blog. You can measure your typing speed in words per minute. Calculates your typing speed based on whether no typos are found in your words. To find out more, go to the blog.
ReplyDeletegörüntülü show
ReplyDeleteücretlishow
R56O
href="https://istanbulolala.biz/">https://istanbulolala.biz/
ReplyDelete5İT
trabzon evden eve nakliyat
ReplyDeletebursa evden eve nakliyat
ordu evden eve nakliyat
erzurum evden eve nakliyat
mardin evden eve nakliyat
HF083
tekirdağ evden eve nakliyat
ReplyDeletekocaeli evden eve nakliyat
yozgat evden eve nakliyat
osmaniye evden eve nakliyat
amasya evden eve nakliyat
D8Q
düzce evden eve nakliyat
ReplyDeletedenizli evden eve nakliyat
kırşehir evden eve nakliyat
çorum evden eve nakliyat
afyon evden eve nakliyat
KLİ7ZZ
4E9AA
ReplyDeleteKayseri Şehirler Arası Nakliyat
Ünye Çatı Ustası
Şırnak Lojistik
Eryaman Parke Ustası
Ankara Lojistik
Erzincan Şehirler Arası Nakliyat
Kocaeli Şehirler Arası Nakliyat
Sinop Şehir İçi Nakliyat
Trabzon Evden Eve Nakliyat
CACC7
ReplyDeleteBartın Evden Eve Nakliyat
Samsun Şehirler Arası Nakliyat
Pi Network Coin Hangi Borsada
Vindax Güvenilir mi
Muğla Şehirler Arası Nakliyat
Bitlis Şehir İçi Nakliyat
Kırklareli Evden Eve Nakliyat
Manisa Şehir İçi Nakliyat
Etlik Boya Ustası
55CB9
ReplyDeleteSivas Şehirler Arası Nakliyat
Çerkezköy Cam Balkon
Çerkezköy Televizyon Tamircisi
Muğla Şehir İçi Nakliyat
Aydın Parça Eşya Taşıma
Edirne Parça Eşya Taşıma
Muş Evden Eve Nakliyat
Tunceli Parça Eşya Taşıma
Ordu Şehir İçi Nakliyat
624DD
ReplyDeleteKeçiören Boya Ustası
Batıkent Parke Ustası
Eryaman Parke Ustası
Çerkezköy Bulaşık Makinesi Tamircisi
Çerkezköy Ekspertiz
Pursaklar Boya Ustası
Ünye Çekici
Coinex Güvenilir mi
Sincan Fayans Ustası
EBFE7
ReplyDeleteBitcoin Nasıl Alınır
Binance Kimin
Btcturk Borsası Güvenilir mi
Binance Para Kazanma
Binance Madenciliği Nedir
Coin Madenciliği Nedir
Kripto Para Nasıl Kazılır
Kripto Para Nasıl Oynanır
Kripto Para Madenciliği Siteleri
E1594
ReplyDeleteBinance Hesap Açma
Coin Madenciliği Siteleri
Bitcoin Madenciliği Nedir
Coin Üretme Siteleri
Coin Madenciliği Nasıl Yapılır
Btcturk Borsası Güvenilir mi
Bulut Madenciliği Nedir
Kripto Para Nedir
Kripto Para Madenciliği Siteleri
455B4
ReplyDeletebinance referans kodu
binance referans kodu
referans kimliği nedir
binance referans kodu
resimli magnet
referans kimliği nedir
resimli magnet
resimli magnet
binance referans kodu
911E5
ReplyDeletebinance referans kodu
resimli magnet
resimli magnet
binance referans kodu
binance referans kodu
referans kimliği nedir
binance referans kodu
resimli magnet
referans kimliği nedir
D4FC0
ReplyDeleteprobit
kaldıraç ne demek
bitexen
referans kodu binance
mexc
canlı sohbet siteleri
https://toptansatinal.com/
bitmex
huobi
8C972
ReplyDeleteprobit
bingx
okex
en iyi kripto para uygulaması
huobi
mercatox
kripto telegram
gate io
binance
2A7F6
ReplyDeletecanlı sohbet
kucoin
telegram kripto para grupları
bitcoin seans saatleri
kucoin
kucoin
kripto para nereden alınır
filtre kağıdı
canli sohbet
05F88
ReplyDeletekripto para nereden alınır
aax
binance referans kimliği nedir
kucoin
toptan mum
bitget
vindax
binance referans kimliği
btcturk
3E715
ReplyDeletebitget
probit
gate io
telegram kripto para kanalları
aax
huobi
huobi
bitcoin hangi bankalarda var
kripto telegram grupları
FA354
ReplyDeleteen az komisyon alan kripto borsası
kraken
July 2024 Calendar
binance
February 2024 Calendar
mexc
January 2024 Calendar
September 2024 Calendar
probit
fghnfgjhnwqwetfreygthruyh
ReplyDeleteتصليح افران جدة
شركة تسليك مجاري بالدمام 6JAgj4RRNQ
ReplyDeleteرقم مصلحة المجاري بالاحساء SqdHhPHzNl
ReplyDelete