C Programming - Functions - Discussion

Discussion Forum : Functions - General Questions (Q.No. 3)
3.
How many times the program will print "IndiaBIX" ?
#include<stdio.h>

int main()
{
    printf("IndiaBIX");
    main();
    return 0;
}
Infinite times
32767 times
65535 times
Till stack overflows
Answer: Option
Explanation:

A call stack or function stack is used for several related purposes, but the main reason for having one is to keep track of the point to which each active subroutine should return control when it finishes executing.

A stack overflow occurs when too much memory is used on the call stack.

Here function main() is called repeatedly and its return address is stored in the stack. After stack memory is full. It shows stack overflow error.

Discussion:
31 comments Page 3 of 4.

Akhil Goyal said:   1 decade ago
I think until stack is overflow (d) is right answer because in stack 1st come last out. When pgm is stored in stack(i.e in memory)firstly main() goes in stack & after this printf will go & then calling main() &after this printf will go again & this process go on till stack not overflow.

Anumita Singha Roy said:   1 decade ago
@Ivo.

Can you explain what do you mean by the sentence "An infinite loop is when we have for example while(1) , where nothing is added to the stack. ".

Ivo said:   1 decade ago
When a program is running and there is a function call, the program adds a pointer to the stack, which points to the the line in the program it must return to once the function returns.
In this case main() calls main() recursively, increasing the stack until it eventually overflows.
An infinite loop is when we have for example while(1), where nothing is added to the stack.

Jeff McCracken said:   1 decade ago
I think answer D should state "Until the stack DOES overflow"

Rakesh said:   1 decade ago
In this program you gave main() recursively and it will be print up to infinite times.Is it correct?or not?

Dnyaneshwar said:   1 decade ago
@Kuldeep Chaudhary

Every called function return value to it's calling function.

Here main() function called by operating system that is main() is called fun and o.s. is calling.

Called fun return 0 after successful execution of program.

If there is any error in execution then it return any nonzero value to it's caller. That value may be +ve or -ve.

Kuldeep chaudhary said:   1 decade ago
What is the diff b/w
#include"...."
and
#include<....>

Kuldeep chaudhary said:   1 decade ago
What is the meaning of return 0 or return 1 in main() function body?

Please tell me. if any one

ex:-
int main()
{
printf("hello");
return 0;
}

Ramesh Vetsa said:   1 decade ago
In the above program we are just printing the statement. we are not declaring any variables and not using any memory. Then why it runs up till stack doesn't overflow?

Sanket said:   1 decade ago
I tried this program on Turbo C++ 4.5 compiler and it gives me error "Cannot call 'main' from within the program in function main()

Also where we used stack here??
Does stack refers to 'Main Memory'?


Post your comments here:

Your comments will be displayed after verification.