Thread: what will be the output and why?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    63

    what will be the output and why?

    it is a very basic and simple problem,but i'm not able to solve it,i hope some1 can help me out in explaning it's output and the use of static keyword here.. ..the output of program is...6 12..i don't know how it came,//? it is also giving me same warning 2 times that function should return a value in line no 9 and 23.
    the program is...
    Code:
    main()
     {
         int a,b;
         a=sumdig(123);
         b=sumdig(123);
         printf("%d%d",a,b)
      }
        sumdig(int n)
    {
            static int s=0;
           int d;
            if(n!=0)
       {
            d=n%10;
            n=(n-d)/10;
            s=s+d;
            sumdig(n);
       }
          else
        return(s);
     }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    static variables retain their value throughout function calls. That is, if the function exits with staticvar = 7, the next call will start with staticvar = 7, etc.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    it is also giving me same warning 2 times that function should return a value in line no 9 and 23.
    Code:
    #include <stdio.h>  /* for printf() etc */
    
    int main()  /* main() returns an int; implicit main() is K&R */
    {
        int a,b;
        a=sumdig(123);
        b=sumdig(123);
        printf("%d%d",a,b)
        return 0;  /* since main() is int, return a value (0 indicates success) */
    }
    
    int sumdig(int n)  /* again, implicit int rule is pre-ANSI */
    {
        static int s=0;
        int d;
        if(n!=0)
        {
            d=n%10;
            n=(n-d)/10;
            s=s+d;
            return sumdig(n);  /* otherwise a garbage value is returned */
        }
        else return(s);
    }
    I changed the indentation too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you just want to understand how static local variables work, try this program:
    Code:
    #include <stdio.h>
    
    void static_func(void);
    void func(void);
    
    int main(void) {
        int x;
    
        for(x = 0; x < 5; x ++) {  /* call static_func() and func() five times */
            static_func();
            func();
        }
    
        return 0;
    }
    
    void static_func(void) {
        static int x = 1;
    
        printf("%i\n", x++);
    }
    
    void func(void) {
        int x = 1;
    
        printf("%i\n", x++);
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Note: static global variables are different. They're just like regular global variables but are only visible in the file in which they are declared. (You can have static functions, too, which act the same way -- one file only.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    thx....but i just wanna know that how would this program be giving the output of 6 12...?
    i couldn't understand that,,i could come up with output as 12 but i couldn't understand how did that output 6.....can u plz explain me the logic behind computing that output as 6 and 12...i'll be really greatful if u can...!!!

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Is this program the one that outputs "6 12"?
    Code:
    #include <stdio.h>
    
    int main()
    {
        int a,b;
        a=sumdig(123);
        b=sumdig(123);
        printf("%d%d",a,b)
        return 0;
    }
    
    int sumdig(int n)
    {
        static int s=0;
        int d;
        if(n!=0)
        {
            d=n%10;
            n=(n-d)/10;
            s=s+d;
            return sumdig(n);
        }
        else return(s);
    }
    Okay, here's the first call:
    Code:
    sumdig(123)
    d = n % 10;    d = 123 % 10;    d = 3;
    n = (n-d)/10;    n = (123-3)/10;    n = 120/10;    n = 12;
    s = s + d;    s = 0 + 3;    s = 3;
    sumdig(12)
    d = n % 10;    d = 12 % 10;    d = 2;
    n = (n-d)/10;    n = (12-2)/10;    n = 10/10;    n = 1;
    s = s + d;    s = 3 + 2;    s = 5;
    sumdig(1)
    d = n % 10;    d = 1 % 10;    d = 1;
    n = (n-d)/10;    n = (1-1)/10;    n = 0/10;    n = 0;
    s = s + d;    s = 5 + 1;    s = 6;
    The second call is messed up because s isn't zero.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    thx ..i could understand that part ,what u just explained,the things in which i'm having a doubts are...:
    that,
    1)why are we taking here sumdig(123),sumdig(12) and sumdig(1)..from where can i come to know in this program that it would have to execute in the order of 123,then 12 and then 1.?....
    and secondly,2)...when would i know that i have to take the value of 6 and 12 amongst so many values from there...which we have computed and 6 is the value of which variable and 12 is the vaue of which variable?...didn't we have to print the values of ,,,a and b..?
    3)which one of the values amongst 6 and 12 is of a and b?
    basically how would the control flow execution would happen in the program..?
    i'm really confused about this....i hope to get a reply from u...that would really be alot of help for me...thanks..waiting for ur reply!

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    1)why are we taking here sumdig(123),sumdig(12) and sumdig(1)..from where can i come to know in this program that it would have to execute in the order of 123,then 12 and then 1.?....
    It's a recursive function. If you mean how does the computer keep track of three different calls to the function, all the local variables for the function call are stored on the stack. Each call has its own distinct d and n (but not s, because s is stored on the heap).
    2)...when would i know that i have to take the value of 6 and 12 amongst so many values from there...which we have computed and 6 is the value of which variable and 12 is the vaue of which variable?...didn't we have to print the values of ,,,a and b..?
    Pardon? Do you mean that you're getting confused with so many variables involved?

    3)which one of the values amongst 6 and 12 is of a and b?
    Well, a is 6 and b is 12. See, b is 6 + whatever-was-in-s (which is 6).
    basically how would the control flow execution would happen in the program..?
    I tried to explain it in my previous post. If that doesn't help, I can try again. Or you could try a debugger.

    You shouldn't take on two things at once, recursive functions and static local variables. Try examining my program above.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    thanks alot...it helped alot..!....thank you my program is working fine now..and i can understand what's it all about...!

Popular pages Recent additions subscribe to a feed