Like Tree3Likes

getting out of for loop

This is a discussion on getting out of for loop within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main() { char* string="ape"; int i=0; for(;string[i]!='

Like Tree3Likes

Thread: getting out of for loop

';i++) { if (string[++i]=='
Like Tree3Likes

Thread: getting out of for loop

') printf("here"); } } output: herehere Can ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    24

    getting out of for loop

    Code:
    #include <stdio.h>
    
    int main() {
       char* string="ape";
       int i=0;
       for(;string[i]!='\0';i++) {
          if (string[++i]=='\0') printf("here");
       }
    }
    output:
    herehere

    Can someone explain what's going on here? Thanks.
    Last edited by gunitinug; 12-11-2011 at 03:32 AM.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by gunitinug View Post
    output:
    herehere
    No...only a single "here" is displayed.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Code:
     if (string[++i]=='\0') printf("here");
    changing the value of i is not a good idea, since the loop does increment i as well and that will result in a problem: an out of bounds array access.
    Salem likes this.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,195
    Just walk around the loop a few times.

    The behaviour is undefined as soon as i reaches 4 and string[i] is accessed. Which will happen, since i is being incremented twice for each execution of the loop body.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    string is pointer its not an array..so in string storing only the starting address of "appe". so it will not get null at the end..
    string[i]=*(string+i);
    in this string contains the starting address, so starting address+i and taking value at that address it may be garbage value..that's why loop is infinity..i think if anything wrong forgive me..

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by gunitinug View Post
    Code:
    #include <stdio.h>
    
    int main() {
       char* string="ape";
       int i=0;
       for(;string[i]!='\0';i++) {
          if (string[++i]=='\0') printf("here");
       }
    }
    output:
    herehere

    Can someone explain what's going on here? Thanks.
    Lets talk about lines 5 and 6 for a minute... this is a very broken way to initialize a for loop. I've seen it multiple times over the last few weeks from various people... so it's likely someone out there is teaching people to do this... and it's WRONG

    Here's why it's wrong...
    Code:
    #include <stdio.h>
    
    int main (void)
      { 
         int x = 0;
         int y = 0;
    
         for(;x < 5; x++)
           for (; y<5; y++)
             printf("* ");
    
        return 0;
    }
    As you can see from the loops this should print 25 stars, right? In fact it prints 5 ... why?
    Because the 2nd through 5th times through the x loop, y is already 5 and the inner loop doesn't run.

    You must ALWAYS initialize a for loop inside the for() statement, that is why they made it that way.
    Last edited by CommonTater; 12-11-2011 at 08:20 AM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by oops.. View Post
    string is pointer its not an array..so in string storing only the starting address of "appe". so it will not get null at the end..
    Yes it gets the null at the end.

    Type this up and try it...
    Code:
    #include <stdio.h>
    
    int main (void)
      {
         char *test = "This is a string";
    
         printf("%s",test);
    
         return 0;
    }

    printf() stops at the null character. If it wasn't there your screen would fill with garbage... so yes literal strings are nul terminated.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    yes i will do..string[i] works?? is it gave any garbage value..in the above output will be this is a string..
    Last edited by oops..; 12-12-2011 at 08:15 AM.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int *string="ape";int i;
    for(i=0;string[i]!='\0';i++)
         {
      if(string[++i]=='\0'
          printf("here");
    }
    getch();
    }
    i tried the about program and the output is just "here"....may be you have ran the program twice....if i am wrong sorry!

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, RamPrasath!

    He's brand new, already posting code between the code tags!

    Sweet!

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,195
    Quote Originally Posted by RamPrasath View Post
    i tried the about program and the output is just "here"....may be you have ran the program twice....if i am wrong sorry!
    Not quite the case, Ram. As I noted in my previous post, the behaviour of the code is undefined, because it is running off the end of the array named string. The word "undefined" has a particular meaning in the C standard, and essentially (in this case) means the code is allowed to do anything according to the standard. Practically, that often means the results can vary between compilers (printing "here" once, multiple times, crashing after printing "here" once, etc).

    This is one of those cases where results of running a program are inconclusive.
    Right 98% of the time, and don't care about the other 3%.

  12. #12
    Registered User ToNy_'s Avatar
    Join Date
    Dec 2011
    Posts
    24
    Code:
    #include <stdio.h> int main() { char* string="ape"; int i=0; for(;string[i]!='\0';i++) { if (string[++i]=='\0') printf("here"); } }
    I think i is a global variable. When i =0 , the loop does nothing.

  13. #13
    Registered User
    Join Date
    Sep 2011
    Posts
    52
    Quote Originally Posted by ToNy_ View Post
    Code:
    #include <stdio.h> int main() { char* string="ape"; int i=0; for(;string[i]!='\0';i++) { if (string[++i]=='\0') printf("here"); } }
    I think i is a global variable. When i =0 , the loop does nothing.
    It's not, couse it's in a function (main)
    ToNy_ likes this.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ToNy_ View Post
    Code:
    #include <stdio.h> int main() { char* string="ape"; int i=0; for(;string[i]!='\0';i++) { if (string[++i]=='\0') printf("here"); } }
    I think i is a global variable. When i =0 , the loop does nothing.
    I is local to the function, declared in the line directly above the for() statment.

    But, As I already explained... you should not initialize a for() statement that way, ever... for() provides a built in initializer step, use it. If you don't there will be situations where this bad practice will cause you trouble...
    ToNy_ likes this.

  15. #15
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by CommonTater View Post
    But, As I already explained... you should not initialize a for() statement that way, ever... for() provides a built in initializer step, use it. If you don't there will be situations where this bad practice will cause you trouble...
    Looks like someone, somewhere grew up on C99/C++ and then fell foul of a non-C99 compiler, where you have to have the declaration at the beginning of the function, and decided the best method was to move the whole initialisation.

    I have to agree that it's horrendously dangerous. If you want to do that, just move the *type* declaration, not the initialisation.

    Code:
    int x;
    for(x = 0; x < 5; x++)
    ........
    But then, they're also incrementing the loop variable from inside for loops without proper thought too.

    To the OP: Your program "works" but only by chance depending on the compiler / system you run it on and even what time of day or what program you last run (if there's something in memory after your string or not). That's not good programming practice and means that although your program works FOR YOU, it doesn't necessarily work for anyone else (even if they have a near-identical computer to yourself).

    A program that small can be stepped through on paper quite simply and overflows like this avoided. Avoid this one while it's simple and the later ones where it takes out the machine or stops 100,000 lines of code from working won't be a problem. Ignore the problem and you'll never be able to write a program that reliably runs on anyone else's computer.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  2. change var inside loop and run loop again
    By d387420489 in forum C Programming
    Replies: 5
    Last Post: 07-29-2011, 01:19 AM
  3. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  4. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  5. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM