Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Behaviour of the program

Reply
Thread Tools

Behaviour of the program

 
 
somenath
Guest
Posts: n/a
 
      03-15-2012

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.




 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      03-15-2012
On Wed, 14 Mar 2012 21:33:36 -0700 (PDT), somenath
<(E-Mail Removed)> wrote:

>
>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.

--
Remove del for email
 
Reply With Quote
 
 
 
 
somenath
Guest
Posts: n/a
 
      03-15-2012
On Mar 15, 11:22*am, Barry Schwarz <(E-Mail Removed)> wrote:
> On Wed, 14 Mar 2012 21:33:36 -0700 (PDT), somenath
>
> <(E-Mail Removed)> wrote:
>
> >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?


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.
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      03-15-2012
"somenath" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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.

--
Bartc

 
Reply With Quote
 
Joe Pfeiffer
Guest
Posts: n/a
 
      03-15-2012
somenath <(E-Mail Removed)> writes:

> Hello All,
>
> I am very much confused with the output of the two following programs.


<snip>

> 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does this program have well-defined behaviour? eq mail C Programming 2 08-13-2012 01:05 AM
strange behaviour of the program. somenath C Programming 8 05-13-2012 04:15 AM
Does this program exhibit undefined behaviour or output? WP C Programming 12 08-27-2007 09:29 AM
Reading from stdin then launching a program that reads from stdin strange behaviour Stefano Sabatini Perl Misc 6 07-29-2007 10:38 PM
debugger behaviour different to execution behaviour Andy Chambers Java 1 05-14-2007 09:51 AM



Advertisments