I don't get what cause the following while loop to terminated

This is a discussion on I don't get what cause the following while loop to terminated within the C Programming forums, part of the General Programming Boards category; The following... Code: #include <stdio.h> #include <stdlib.h> #include <utmp.h> int main(void) { FILE *f; int n; char buffer[1024]; f=fopen(_PATH_UTMP,"rb"); if ...

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    I don't get what cause the following while loop to terminated

    The following...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <utmp.h>
    
    int main(void) {
      FILE *f;
      int n;
      char buffer[1024];
    
      f=fopen(_PATH_UTMP,"rb");
      if (f==NULL) exit(0);
    
      while (1) { /* trademark bartc loop */
    
        n=fread(buffer,1,1024,f);
        printf("N = %d EOF = %d\n",n,feof(f));
        if (n<1024) {
          printf("no more\n");
          break;
        }
      }
    
      fclose(f);
    
    }
    produces..

    [cd@localhost oakland]$ gcc -g bart.c -o bart
    [cd@localhost oakland]$ ./bart
    N = 1024 EOF = 0
    N = 1024 EOF = 0
    N = 1024 EOF = 0
    N = 1024 EOF = 0
    N = 1024 EOF = 0
    N = 1024 EOF = 0
    N = 1024 EOF = 0
    N = 512 EOF = 1
    no more
    [cd@localhost oakland]$
    The question is, what is while being compared against to make this loop terminate. I mean,

    Code:
    while(1){ }
    Is just

    Code:
    while(1 != 0){ }
    So what is getting compared against not zero in this case?

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    957
    It's the "break". That will terminate the (innermost) loop you're in, regardless of whether the condition is true or false.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    whats actually happening is it terminates when it fails to read an entire 1024 bytes from the file, in which case it executes the break statement which exits the current loop.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  4. #4
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Okay, I see it now. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21