Thread: Idiot guide

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48

    Question Idiot guide

    Hi,
    I am just beginning to learn C and I have come across a program that I just can't understand the result of. I was hoping someone would be so kind as to explain it to me and I am sure it is pretty simple. I understand the concept of the the for statement but this program has stumped me.
    *edited
    Code:
    int i = 9;
    for (i--; i--; i--)
        printf("%d", i);
    The output is : 7 5 3 1

    I am confused from the beginning here. i is initially 9 and in the for statement it is initialized as 1++ which I thought would mean it is 8.

    Thanks.
    Last edited by deadhippo; 03-04-2008 at 11:59 PM. Reason: Fix mistakes

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    That is not the output I get. Are you sure it isn't this:
    Code:
    int i = 9;
    for (i--; i--; i--)
        printf("%d", i);
    Don't quote me on that... ...seriously

  3. #3
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Code:
    int i = 9;
    for (i++; i++; i++)
        printf("%d", i);
    It is an infinite loop.

    I think you are trying to make this:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int i = 9;
        for (i++; i>0; i--)
            printf("\n%d", i);
    	
    	return 0;
    }
    Last edited by abh!shek; 03-04-2008 at 11:20 PM.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Hi, thanks for the speedy replies.
    Brad is right. I got a bit carried away when I was typing. I ought to be more careful.
    Sorry.
    Please exchange '8' for '10' in my previous post.
    I was wondering why I felt more confused when I posted this.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    It is an infinite loop.
    Not so infinite... Just till i makes the whole loop and gets back to zero
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    abyss - deep C
    Join Date
    Oct 2007
    Posts
    46

    Thumbs up

    below is the equivalent while loop, this could give you an idea :-)

    Code:
    int i = 9;
    
    i--;                             // initialization
    while(i--){                 // check the condition
        printf("%d ", i);
        i--;                         // iteration
    }
    cheers
    maverix

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or maybe

    Code:
    int i = 9;
    
    i--;
    while(i)
    {
    	i--;
    	printf("&#37;d ", i);
    	i--;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Or maybe

    Code:
    int i = 9;
    
    i--;
    while(i)
    {
    	i--;
    	printf("%d ", i);
    	i--;
    }
    That's not quite right - i will be zero at the end of that loop, whilst i will be -1 at the end of the loops posted above. Since the i-- is part of the condition of the while-loop, it's going to be executed even when i is zero, whilst your i-- being inside the loop is not executed when i is not true (i == 0).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm not sure how the assembly does it, though.
    Basically,
    Code:
    mov esi, i;
    dec esi;
    jne /*jump*/;
    Does jne check esi or some such?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Thanks again for your replies and sorry I didn't reply sooner. Just back from work.
    And now I am more confused than before. I think I need it really broken down to understand it. This what I think and why I am confused I guess.

    I read that "the first time the application encounters the loop, the initial expression will be executed". i is declared as int with a value of 9.

    When the application encounters the loop it is i-- so if you told me it printed 8 I would believe you except that it doesn't. The second part of the loop is supposed to be a condition but I can't figure out how i-- is a condition which makes it hard for me to understand the rest of the program.

    Then finally the the third part of the loop is ... I think I get it.

    Let me try and maybe someone can laugh at my theory and hopefully tell me where I have gone wrong.

    In the for loop the i is immediately decremented to 8, then the condition i-- is checked and as i-- is not 0 that means that the statement must be true so the concluding expression is executed decrementing it further to 7, which is printed. This continues down to 5, 3 and 1 and the one is checked again and is decremented to 0 but the condition is checked and it is also 0 and therefor false so the concluding expression is not checked and the loop ends.

    How did I do? Don't laugh too hard. I'm very sensitive.

  11. #11
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by Elysia View Post
    I'm not sure how the assembly does it, though.
    Basically,
    Code:
    mov esi, i;
    dec esi;
    jne /*jump*/;
    Does jne check esi or some such?
    It sets the CPU flags, specifically the zero flag, if esi went to 0. The CPU flags are modified with every Arithmetic - Logic operation, and some other operations also.
    test eax,edx;
    jz XXX;
    for example will test for 0, without altering eax, while the alternative would be :
    sub eax,edx;
    jnz EEE;
    or whatever...
    Get a copy of ollydbg and run a simple executable through it, noting the operations and their effect on CPU flags will be much more enlightening to you probably given your experience.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by deadhippo View Post
    In the for loop the i is immediately decremented to 8, then the condition i-- is checked and as i-- is not 0 that means that the statement must be true so the concluding expression is executed decrementing it further to 7, which is printed. This continues down to 5, 3 and 1 and the one is checked again and is decremented to 0 but the condition is checked and it is also 0 and therefor false so the concluding expression is not checked and the loop ends.
    Yes, that's right. The condition is checked before the loop body is entered.

    Quote Originally Posted by xuftugulus View Post
    It sets the CPU flags, specifically the zero flag, if esi went to 0. The CPU flags are modified with every Arithmetic - Logic operation, and some other operations also.
    test eax,edx;
    jz XXX;
    for example will test for 0, without altering eax, while the alternative would be :
    sub eax,edx;
    jnz EEE;
    or whatever...
    Get a copy of ollydbg and run a simple executable through it, noting the operations and their effect on CPU flags will be much more enlightening to you probably given your experience.
    You're right...
    I would normally think you would have to do:
    Code:
    dec esi;
    cmp esi, 0;
    je /* test */;
    Assembly is so complicated whenever all operations modify the flags. It's part of what I don't understand.
    Maybe I'll do that someday.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Sorry to tag a follow on to this thread but I have a similar program which doesn't work out using my previous "logic".

    int i;
    for(i = 5; ++i; i -= 3)
    printf("%d ", i);

    following the logic from my precious program i is initialized as 5, then the condition ++i is checked and it is true so the concluding expression is executed and 2 will be printed. i is now 2 and the ++i condition is checked and it is true so the concluding expression is executed resulting in -1

    I'm not too sure where I would go next but it doesn't really matter as I have apparently messed up from the beginning because the output is 6 4 2.

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your code is equivalent to
    Code:
    int i = 5;
    ++i;
    while(i)
    {
       printf("&#37;d ", i);
       i -= 3;
       ++i;
    }
    do you see now the reason for the output you receive?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Stop writing such retarded loops is my advice.
    Generally, I'd avoid using ++ or -- as loop condition. Do that in the next field that allows you to execute code after each loop. This applies to for loops.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free C Learning Guide.
    By djnorthyy in forum C Programming
    Replies: 2
    Last Post: 04-05-2008, 02:13 AM
  2. programming for interactive tv guide
    By newprogrammer27 in forum C Programming
    Replies: 1
    Last Post: 04-05-2007, 08:57 AM
  3. Survival Guide
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-14-2004, 02:08 AM
  4. graphic process with vc++ guide?
    By toysoldier in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2004, 12:50 AM
  5. Screaming idiot - shuttle "photo".
    By adrianxw in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-08-2003, 08:44 PM