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;
}
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 1 of 4.
Shiny vn said:
1 decade ago
Here we need to understand that it is a recursive call of a function.
(main() is also a function so it exhibits all characteristics of a function, you can understand that by knowing declaration of a function.
<return type> <function> <arguments> <function body>).
Now comes STACK, space in stack is allocated when a function call is done, to save the location where it should return when statements in function are executed.
The function main() has a statement which is calling itself without any condition. So each time the function is called space is allocated in stack. So stack space is allocated until there is no space left.
(main() is also a function so it exhibits all characteristics of a function, you can understand that by knowing declaration of a function.
<return type> <function> <arguments> <function body>).
Now comes STACK, space in stack is allocated when a function call is done, to save the location where it should return when statements in function are executed.
The function main() has a statement which is calling itself without any condition. So each time the function is called space is allocated in stack. So stack space is allocated until there is no space left.
(2)
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.
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.
Vin... said:
1 decade ago
@Kavitha: Your program has syntax errors:
1) Complete the double Quote.
2) Use \n for new line you used /n.
Please make it correct or run this program on Dec C compiler.
OUTPUT is : Return value of f(11100) is: 0.
void main()
{
printf("Return value of f(%d) is %d:\n",11100,f(11100));
}
int f(int n)
{
if(n<=0)
return 0;
else
1+f(n/10);
}
1) Complete the double Quote.
2) Use \n for new line you used /n.
Please make it correct or run this program on Dec C compiler.
OUTPUT is : Return value of f(11100) is: 0.
void main()
{
printf("Return value of f(%d) is %d:\n",11100,f(11100));
}
int f(int n)
{
if(n<=0)
return 0;
else
1+f(n/10);
}
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.
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.
Bhavadip Gothadiya BG said:
8 years ago
#include<...> // it syntax is used for when Attached the header file into our program
#include" ... " //it syntax is used when we attached the user-defined header file with our program.
Similarly, it both are same
but it Uniquely identify the User-defined header file and inbuilt in Cpp or C compiler Header file.
#include" ... " //it syntax is used when we attached the user-defined header file with our program.
Similarly, it both are same
but it Uniquely identify the User-defined header file and inbuilt in Cpp or C compiler Header file.
Srini said:
1 decade ago
@Kuldeep chaudhary.
#include< >- It means that the pre-processor pre-compiles the system files and extracts the definitions from that file.
#include" "- Same process takes place and the only difference is that, it considers the name, which is defined inside the "" as an user defined file.
#include< >- It means that the pre-processor pre-compiles the system files and extracts the definitions from that file.
#include" "- Same process takes place and the only difference is that, it considers the name, which is defined inside the "" as an user defined file.
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.
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'?
Also where we used stack here??
Does stack refers to 'Main Memory'?
Nath said:
4 years ago
I Think. Here we have to put a base case in function which is called, then only we could get an answer stack overflow. But here in this code, no base case is added so, the answer should be infinite times.
(1)
Kavitha said:
1 decade ago
Can any one help me out in solving the following problem?
void main()
{
printf("Return value of f(%d) is %d/n,11100,f(11100));
}
int f(int n)
{
if(n<=0)
return 0;
else
1+f(n/10);
}
void main()
{
printf("Return value of f(%d) is %d/n,11100,f(11100));
}
int f(int n)
{
if(n<=0)
return 0;
else
1+f(n/10);
}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers