473,387 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Stack & recursion Question

int main()
{
printf("Input a line: ");
wrt_it();
printf("\n\n");
return 0;
}

void wrt_it()
{
int c;

if (( c = getchar()) != '\n')
wrt_it();
putchar(c);
}

the above is from 'A book on C'
and it says the function calls are stacked.

i know what stack means. but don't understand how stack is related to
recursion.

Could anyone explain to me about that ? and, why does the input
characters print reversely?
Nov 13 '05 #1
5 10207
Stack is the memory portion which linker determines for the executable to
use. It's the place for arguments, return address and local variables when a
function is called.

: i know what stack means. but don't understand how stack is related to
: recursion.

Your program comsumes stack space every time functions are called.

: Could anyone explain to me about that ? and, why does the input
: characters print reversely?

Think about the way how the function is called. A debugger may help you.

Nov 13 '05 #2
"herrcho" <he*********@kornet.net> schrieb im Newsbeitrag
news:bl**********@news1.kornet.net...
int main()
{
printf("Input a line: ");
wrt_it();
printf("\n\n");
return 0;
}

void wrt_it()
{
int c;

if (( c = getchar()) != '\n')
wrt_it();
putchar(c);
}

the above is from 'A book on C'
and it says the function calls are stacked.

i know what stack means. but don't understand how stack is related to
recursion.

Could anyone explain to me about that ? and, why does the input
characters print reversely?


First: the language does not say anything about how parameters are passed.
Fortunately your book says " function calls are stacked", which IMHO has not
necessarily to do with a "stack" like it is used in many, but not all
implementations.
Anyway, parameters must be stored in a last in first out way somehow...

now to your question:
void wrt_it()
{
int c;

if (( c = getchar()) != '\n')
wrt_it();
putchar(c);
}
wrt_it is called from main(), it gets a local copy of c
wrt_it receives one character and stores it in c
let's assume it is not '\n' then
wrt_it calls itself, this new instance gets it's own local copy of
c, to help understanding let's name it c1
wrt_it receives one character and stores it in c1
let's assume it is not '\n' then
wrt_it calls itself, this new instance gets it's own local
copy of c, to help understanding let's name it c2
wrt_it receives one character and stores it in c2
let's assume it is '\n' now, then
wrt_it does not call itself anymore, instead it outputs it's
copy of c (c2)
and returns to the previous instance of itself
the previous instance also outputs _it's_c which is c1 and
returns to the previous instance of itself
this instance again outputs it's own copy of c and returns to the
caller (main() in our example
we are done

Ngngng - hope I got it right (including the formatting) :)
Robert


Nov 13 '05 #3
herrcho wrote:

int main()
{
printf("Input a line: ");
wrt_it();
printf("\n\n");
return 0;
}

void wrt_it()
{
int c;

if (( c = getchar()) != '\n')
wrt_it();
putchar(c);
}

the above is from 'A book on C'
and it says the function calls are stacked.

i know what stack means. but don't understand how stack is related to
recursion.

Could anyone explain to me about that ? and, why does the input
characters print reversely?


Let me recommend my column on this subject from "Computing in
Science and Engineering". Title is "Recurses!" Can be found at

http://www.phys.virginia.edu/classes...ogs/Cprogs.htm

Basically, languages that implement recursion use stacks to pass
parameters and return addresses when calling subroutines (or functions).
How this is done, exactly, is left up to the implementer. But a stack
of some sort must be used for recursion. (It may or may not be the cpu
stack, depending on the cpu, etc.)

--
Julian V. Noble
Professor Emeritus of Physics
jv*@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
Nov 13 '05 #4
bd
Julian V. Noble wrote:
herrcho wrote:

int main()
{
printf("Input a line: ");
wrt_it();
printf("\n\n");
return 0;
}

void wrt_it()
{
int c;

if (( c = getchar()) != '\n')
wrt_it();
putchar(c);
}

the above is from 'A book on C'
and it says the function calls are stacked.

i know what stack means. but don't understand how stack is related to
recursion.

Could anyone explain to me about that ? and, why does the input
characters print reversely?
Let me recommend my column on this subject from "Computing in
Science and Engineering". Title is "Recurses!" Can be found at

http://www.phys.virginia.edu/classes...ogs/Cprogs.htm
Basically, languages that implement recursion use stacks to pass
parameters and return addresses when calling subroutines (or functions).
How this is done, exactly, is left up to the implementer. But a stack
of some sort must be used for recursion. (It may or may not be the cpu
stack, depending on the cpu, etc.)


Dosen't Continuation Passing Style not use a stack? Or am I confused about
it?

--
Fifth Law of Procrastination:
Procrastination avoids boredom; one never has the feeling that
there is nothing important to do.

Nov 13 '05 #5
On Thu, 02 Oct 2003 19:02:35 -0400, bd wrote:
Julian V. Noble wrote:
Basically, languages that implement recursion use stacks to pass
parameters and return addresses when calling subroutines (or functions).
How this is done, exactly, is left up to the implementer. But a stack
of some sort must be used for recursion.


Dosen't Continuation Passing Style not use a stack? Or am I confused
about it?


CPS uses a continuation, which grows until it is executed.

What's important is that tail-recursive calls don't have to grow
the stack. A tail-recursive function in C might well blow the
stack, but that's a problem with the implementation, not a problem
with recursion.

-Sheldon

Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: chandra.somesh | last post by:
I recently was having trouble converting implementaion of recursions using stack. While single level of recursions are quite intuitive , it is when we have more than one recursive calls in the...
48
by: Michael Sig Birkmose | last post by:
Hi everyone! Does anyone know, if it is possible to meassure the maximum stack usage of a C program throughout it's entire execution? -- Michael Birkmose
5
by: ip4ram | last post by:
I am quite puzzled by the stack overflow which I am encountering.Here is the pseudocode //define stack structure //function operating on stack void my_stack_function( function parameters) {...
2
by: jw | last post by:
what is the relation between a stack and recursion func. and if i have recursion funct(a return type void), void print(int n,int base){ static string digits="0123456789abcdef"; if(n>=base){...
13
by: deepak | last post by:
Hi In the following function how the memory 'll be allocated. 1) Will it allocate memory for all the char's together or allocate for first char. then for int then for float and after this only...
13
by: jm.suresh | last post by:
Hi, I have a program which literately finds the object that overlapping a point. The horizontal and vertical search are called recursively from inside each other. Is this way of implementation...
2
by: PJ6 | last post by:
I have an algorithm that processes data recursively. When I'm testing it works fine, but when I feed it data from the actual application, I get "stack overflow". What bothers me is that the data my...
4
by: lianne.ribeiro | last post by:
I am using Visual C++ 6 IDE,with 512MB RAM. I have coded a recursive function that has a correct end condition for recursion. There is no infinite recursion. For some input data,the recursive...
62
by: jt | last post by:
hello everyone.. int fun() { /* anything */ } int main(void) { fun(); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.