# Recursive call to main() in C



## rijinpk1 (Jun 28, 2012)

Write a c pgm which rewrites "The universe is never ending" using recursion so that it terminate after 17 calls. Your pgm should consist of a single main() function that calls itself recursively.
This question seems to be simple but asked for 15 marks. So I need answers from u guys.
Help me. Thanks in advance.


----------



## sakumar79 (Jun 28, 2012)

*Re: help in a C program*

Why dont you try to answer it yourself and see if you get errors. If you do, try to solve them yourself and only if you cannot solve a problem, post your code and note where you have a problem...

Please dont use the forum to solve your assignments for you.

Arun


----------



## krishnandu.sarkar (Jun 28, 2012)

*Re: help in a C program*

Yes it's simple. I'd say try it yourself and post your attempt. No one would write your program for your assignment.

But if you do mistakes, everyone will help you.


----------



## rijinpk1 (Jun 28, 2012)

*Re: help in a C program*

This question is asked in my theory exam for the subject computer programming in engineering... Not in pratical xam.
What I actully written in the xam is given below.

```
#include<stdio.h>
int main()
{
 static int i=0;
 while(i<17)
 {
    printf("THE  UNIVERSE IS NEVER ENDING");
 i**;
 main();
 }
}
```
read * as plus symbol. I dont know why it is not printed here. 
is this the right answer? If yes then does this question deserves 15 marks?


----------



## RBX (Jun 29, 2012)

*Re: help in a C program*

You need to declare and define a counter before entering recursion, you can implement termination condition within method's body.


----------



## krishnandu.sarkar (Jun 29, 2012)

*Re: help in a C program*


```
#include<stdio.h>
int main()
{
    static int i = 0;
    rec_func();
    return 0;
}

void rec_func()
{
    if(i>=17)
        return;
    else
    {
        print("THE UNIVERSE IS NEVER ENDING");
        i++;
        rec_func();
    }
}
```

Should be something like this.


----------



## vickybat (Jun 29, 2012)

*Re: help in a C program*

Its offtopic but here's the java version:


```
public class Universe {
public static void univ(int i){
	
	if ( i <= 16 ){
	System.out.println("The universe is never ending");
	
	univ(i+1);
	
	}
}
	
	public static void main(String [] args ){
		
		univ(0);
	}
}
```


----------



## rijinpk1 (Jun 29, 2012)

*Re: help in a C program*



RazorbladeXtreme said:


> You need to declare and define a counter before entering recursion, you can implement termination condition within method's body.



I could not understand you. Can you explain in detail? What is a counter?



krishnandu.sarkar said:


> ```
> #include<stdio.h>
> int main()
> {
> ...



Is  my  answer wrong?
Is there any mistakes?
In the question it is clear that the main() function should call it recursively. So yours is wrong?
Pls clarify.
I wrote only this question. Did not consider the "OR" question. Will my 15 marks be waived off?


----------



## Hrishi (Jun 29, 2012)

*Re: help in a C program*



rijinpk1 said:


> I could not understand you. Can you explain in detail? What is a counter?



Counter is actually any integer variable (e.g. int ctr=0 which is used in a loop to find out how many times the loop has actually revolved around.

for e.g.
int ctr=0;
int x=0;
for(x=0;x<5;x++)
 {
 ctr++;
 }

the output will be 
ctr=5 since the loop has revolved for 5 times.

I hope you got it.


----------



## pulkitpopli2004 (Jun 29, 2012)

*Re: help in a C program*



rijinpk1 said:


> #include<stdio.h>
> void main()
> {
> static int i=0;
> ...



yes this is completely correct.. A small thing you can get or teacher can deduct mark for is "\n" in printf command.. 
You should have written that.


----------



## krishnandu.sarkar (Jun 29, 2012)

*Re: help in a C program*



rijinpk1 said:


> I could not understand you. Can you explain in detail? What is a counter?
> 
> 
> 
> ...



I'm not too good in C 

I'd like to wait for some other member to verify that.

But I don't know whether main() can call itself.


----------



## Vyom (Jun 29, 2012)

Yes, main function can call itself. Afterall main is also a function. And just like other function main also can accept parameters and return values.
Try attempting the conversion of that recur function to main. It should be simple.

 Posted from a mobile device.


----------



## rijinpk1 (Jun 29, 2012)

*Re: help in a C program*



pulkitpopli2004 said:


> yes this is completely correct.. A small thing you can get or teacher can deduct mark for is "\n" in printf command..
> You should have written that.



I actually put "\n" in my answer in the xam. But forgot to put here.
But my question is does this question deserve 15 marks? I absolutely dont think so...



krishnandu.sarkar said:


> I'm not too good in C
> 
> I'd like to wait for some other member to verify that.
> 
> But I don't know whether main() can call itself.



yes.. main() can call itself recursively if needed.


----------



## sarthak (Jun 29, 2012)

*Re: help in a C program*



rijinpk1 said:


> But my question is does this question deserve 15 marks? I absolutely dont think so...



You can expand letters or articles or reports in english if you feel that they are small, but you can't do the same with a computer program. There is nothing like minimum or maximum word limit here.
The 15 marks are for the logic, or the teachers want to make passing easy.


----------



## krishnandu.sarkar (Jun 29, 2012)

*Re: help in a C program*

Thanks guys for confirming the same.


----------



## RBX (Jun 29, 2012)

*Re: help in a C program*

Sorry, didn't read the question carefully. Since it requires recursion of main(), you need a static counter declared obviously inside the main()'s body as you have done in post #4, it just doesn't need a while loop, but a selection (if..else, switch-case) which terminates the loop when counter reaches 17.



Spoiler



You shouldn't be seeing this


Spoiler





```
#include<stdio.h>
int main(void) {
	static int i=0; //This is counter
	if(i==16) return 0; //Termination Condition
	printf("The World is Never Ending\n");
	i++; //Counting : )
	main();
}
```


----------



## Hrishi (Jun 29, 2012)

*Re: help in a C program*

Recrusion of main() in C++ is not legal.The compiler will produce error.

HOwever , in C you are allowed to do it but its little complicated in barely used.


----------



## rijinpk1 (Jun 29, 2012)

*Re: help in a C program*



Rishi. said:


> Recrusion of main() in C++ is not legal.The compiler will produce error.
> 
> HOwever , in C you are allowed to do it but its little complicated in barely used.



I didnt know that. I will check it for sure. Need to clarify that asap. Anyway thanks for commenting.


----------



## nbaztec (Jun 29, 2012)

*Re: help in a C program*



rijinpk1 said:


> I didnt know that. I will check it for sure. Need to clarify that asap. Anyway thanks for commenting.



Rishi is right. Unlike C, recursion of main() is invalid in C++.


----------



## RBX (Jul 1, 2012)

*Re: help in a C program*



sarthak said:


> The 15 marks are for the logic, or the teachers want to make passing easy.



Never happens with me. I write just what's asked in the question, but unless you explain history of every term encountered in the question, you've got very slim chances of scoring


----------



## rijinpk1 (Jul 2, 2012)

*Re: help in a C program*



RazorbladeXtreme said:


> Never happens with me. I write just what's asked in the question, but unless you explain history of every term encountered in the question, you've got very slim chances of scoring



absolutely true for all UNIVERSITY exams.....


----------



## audiophilic (Jul 3, 2012)

*Re: help in a C program*



sakumar79 said:


> Why dont you try to answer it yourself and see if you get errors. If you do, try to solve them yourself and only if you cannot solve a problem, post your code and note where you have a problem...
> 
> Please dont use the forum to solve your assignments for you.
> 
> Arun



I was about to say the same thing.


----------



## rijinpk1 (Jul 3, 2012)

*Re: help in a C program*

^^ no chance this time. Best of luck next time.


----------

