Behaviour of the program

S

somenath

Hello All,

I am very much confused with the output of the two following programs.

First program
==================
#include<stdio.h>

void printd(int n)
{
printf("\beginning n = %d\n",n);
if (n/10) {
// n = n/10;
printd(n/10);
}
printf("%c ",((n%10)+'0'));

}
int main(void)
{
printd(123);
putchar('\n');
return 0;
}
Output
../a.out

beginning n = 123

beginning n = 12

beginning n = 1
1 2 3

Second program

#include<stdio.h>

void printd(int n)
{
printf("\beginning n = %d\n",n);
if (n/10) {
n = n/10;
printd(n);
}
printf("%c ",((n%10)+'0'));

}
int main(void)
{
printd(123);
putchar('\n');
return 0;
}

Output
==============================
beginning n = 123

beginning n = 12

beginning n = 1
1 1 2
The only difference between the two program is as follows
In the first program printd is called with n/10 as argument but in the
latter after computing n=n/10 printd is being called recursively .

Then why the two program output differently?

So my understanding for this different behaviour as follows.

In case of second program the n=n/10 is changing the original n which
was pushed to stack . That is
For printd(123) n is modified to 12
For printd(12) n is modified to 1

so when if (n /10 ) fails it prints 1(1%10 = 1) , then for the call
printd(12) , prints 1 (as n which was 12 but latter modified to 1
so 1%10 =1 ) then similarly it print 12%10 2 for call printd(123)

Please let me know if my understanding is correct ? Please provide
some inputs.
 
B

Barry Schwarz

Hello All,

I am very much confused with the output of the two following programs.

First program
==================
#include<stdio.h>

void printd(int n)
{
printf("\beginning n = %d\n",n);

I don't understand how the \b at the beginning of your format string
can produce the character b you show in your sample output.
if (n/10) {
// n = n/10;
printd(n/10);
}
printf("%c ",((n%10)+'0'));

}
int main(void)
{
printd(123);
putchar('\n');
return 0;
}
Output
./a.out

beginning n = 123

Where is that b coming from? It is not in your format string.
beginning n = 12

I don't understand why your code produces double spacing here.

Did you cut and paste your code and output or did you retype it?
beginning n = 1
1 2 3

Second program

#include<stdio.h>

void printd(int n)
{
printf("\beginning n = %d\n",n);
if (n/10) {
n = n/10;
printd(n);
}
printf("%c ",((n%10)+'0'));

}
int main(void)
{
printd(123);
putchar('\n');
return 0;
}

Output
==============================
beginning n = 123

beginning n = 12

beginning n = 1
1 1 2
The only difference between the two program is as follows
In the first program printd is called with n/10 as argument but in the
latter after computing n=n/10 printd is being called recursively .

Then why the two program output differently?

So my understanding for this different behaviour as follows.

In case of second program the n=n/10 is changing the original n which
was pushed to stack . That is
For printd(123) n is modified to 12
For printd(12) n is modified to 1

so when if (n /10 ) fails it prints 1(1%10 = 1) , then for the call
printd(12) , prints 1 (as n which was 12 but latter modified to 1
so 1%10 =1 ) then similarly it print 12%10 2 for call printd(123)

Please let me know if my understanding is correct ? Please provide
some inputs.

Your understanding is basically correct except for your references to
the stack. While that may be how your system performs parameter
passing, the language only requires each recursive execution of printd
to have its own private copy of n. If you have optimization turned
on, n could be a register variable. How the system creates each n at
the entry to printd, keeps them separate during execution, and
destroys each n as its function returns is an implementation detail
not addressed by the standard.
 
S

somenath

I don't understand how the \b at the beginning of your format string
can produce the character b you show in your sample output.












Where is that b coming from?  It is not in your format string.




I don't understand why your code produces double spacing here.

Did you cut and paste your code and output or did you retype it?

Sorry for the mistake. While posting the program I realized the there
is a spelling mistake. so without changing the real program I tried to
change the code in post. Very sorry for that.I will be cautious about
this in feature.
 
B

BartC

Second program

#include<stdio.h>

void printd(int n)
{
printf("\beginning n = %d\n",n);
if (n/10) {
n = n/10;
printd(n);
}
printf("%c ",((n%10)+'0'));

You're dividing n by 10, then taking the remainder of that new n in n%10.

So if n is 142857, n/10 is true, so you assign n=142857/10 or n=14285, and
make a recursive call. Then print 14285%10 or 5.

The '7' digit is lost forever.
 
J

Joe Pfeiffer

somenath said:
Hello All,

I am very much confused with the output of the two following programs.

The only difference between the two program is as follows
In the first program printd is called with n/10 as argument but in the
latter after computing n=n/10 printd is being called recursively .

Then why the two program output differently?

So my understanding for this different behaviour as follows.

In case of second program the n=n/10 is changing the original n which
was pushed to stack . That is
For printd(123) n is modified to 12
For printd(12) n is modified to 1

so when if (n /10 ) fails it prints 1(1%10 = 1) , then for the call
printd(12) , prints 1 (as n which was 12 but latter modified to 1
so 1%10 =1 ) then similarly it print 12%10 2 for call printd(123)

Please let me know if my understanding is correct ? Please provide
some inputs.

You seem to have figured out what's going on; why are you still
confused?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top