Behaviour of the program

Discussion in 'C Programming' started by somenath, Mar 15, 2012.

  1. somenath

    somenath Guest

    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.
     
    somenath, Mar 15, 2012
    #1
    1. Advertising

  2. On Wed, 14 Mar 2012 21:33:36 -0700 (PDT), somenath
    <> 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
     
    Barry Schwarz, Mar 15, 2012
    #2
    1. Advertising

  3. somenath

    somenath Guest

    On Mar 15, 11:22 am, Barry Schwarz <> wrote:
    > On Wed, 14 Mar 2012 21:33:36 -0700 (PDT), somenath
    >
    > <> 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.
     
    somenath, Mar 15, 2012
    #3
  4. somenath

    BartC Guest

    "somenath" <> wrote in message
    news:...

    > 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
     
    BartC, Mar 15, 2012
    #4
  5. somenath

    Joe Pfeiffer Guest

    somenath <> 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?
     
    Joe Pfeiffer, Mar 15, 2012
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Chambers
    Replies:
    1
    Views:
    410
    Daniel Dyer
    May 14, 2007
  2. WP
    Replies:
    12
    Views:
    459
    Richard Bos
    Aug 27, 2007
  3. Stefano Sabatini
    Replies:
    6
    Views:
    316
    Stefano Sabatini
    Jul 29, 2007
  4. somenath

    strange behaviour of the program.

    somenath, May 3, 2012, in forum: C Programming
    Replies:
    8
    Views:
    406
    David Thompson
    May 13, 2012
  5. eq mail
    Replies:
    2
    Views:
    385
    Eric Sosman
    Aug 13, 2012
Loading...

Share This Page