Thread: output misunderstanding

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    32

    output misunderstanding

    I'm having trouble understanding the output of this code:

    Code:
    #include <stdio.h>
    
    void a(int num);
    void b(int num);
    
    int main() {
         a(6);
         return 0;
    }
    
    void a(int num) {
         int index;
         for (index=0; index<num; index++)
              b(index);
    }
    void b(int num) {
         int index=0;
         for (index=0; index<num; index++)
              printf("%d", index);
    }

    Why is it 0012012012301234?
    Why is b(0) and b(1) not taken to the account?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Neotriz View Post
    I'm having trouble understanding the output of this code:

    Code:
    #include <stdio.h>
    
    void a(int num);
    void b(int num);
    
    int main() {
         a(6);
         return 0;
    }
    
    void a(int num) {
         int index;
         for (index=0; index<num; index++)
              b(index);
    }
    void b(int num) {
         int index=0;
         for (index=0; index<num; index++)
              printf("%d", index);
    }

    Why is it 0012012012301234?
    Why is b(0) and b(1) not taken to the account?
    It's impossible to answer the question "why is the output 0012012012301234", because the output is not "00120123012301234".

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    32
    Quote Originally Posted by tabstop View Post
    It's impossible to answer the question "why is the output 0012012012301234", because the output is not "00120123012301234".
    Sorry, I meant 001012012301234

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, what else did you expect? (Trace through the logic: what is printed for b(0), for instance?)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM