The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+3 votes
158 views
main () {
    if(--i) {
        main ();
        printf("%d", i);
    }
}
  1.   5 4 3 2 1
  2.   1 2 3 4 5
  3.   0 0 0 0 0
  4.   Compiler error
asked in Programming by Boss (13.5k points)
edited by | 158 views
+1
stack overflow

So, compiler error.

main() will call infinite time
0
but here 'i is not defined,and even if we assume i to be 0 as default value then why will it enter if() loop as condition will be false
+1

yes yes that error will show

http://ideone.com/3zZP0r

1 Answer

+2 votes
Best answer
// assuming i is defined
// output 4 zeros
#include <stdio.h>
int i=5;
int main() {
	if(--i) {
		main();
		printf("%d\n",i);
	}
}

// assuming i is defined
// runtime stack overflow :  error
#include <stdio.h>
int main() {
	int i=5;
	if(--i) {
		main();
		printf("%d\n",i);
	}
}

// assuming i is defined
// output 4 zeros
#include <stdio.h>
int main() {
	static int i=5;
	if(--i) {
		main();
		printf("%d\n",i);
	}
}

 

answered by Veteran (56.2k points)
selected by
0
@debashish,why is there an error in your second example..??because in every call,i will be equal to 5..right?
0
not error ..only stackoverflow
0
and what about my question??if nothing is mentinoed for 'i',then what will be the answer?
0

'i' undeclared error should be .. or what else can be? :)

0
in the third example, static int 5 , why its outputting 4 zeroes?

my doubt is that after five levels in the recursion tree, value of i becomes 0 and if condition becomes false.

please explain?


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

34,769 questions
41,728 answers
118,865 comments
41,379 users