why does while(1) does not work?

This is a discussion on why does while(1) does not work? within the C Programming forums, part of the General Programming Boards category; Hello everybody, I'm a C programming beginner I was asking myself why this code (it compiles properly) prints only zeros ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Reggio Emilia, Italy
    Posts
    15

    why does while(1) does not work?

    Hello everybody,

    I'm a C programming beginner
    I was asking myself why this code (it compiles properly) prints only zeros
    Code:
    #include <stdio.h>
    
    int main()
    {
    
      int number = 2;
    
      while (1)
      {
    	printf("&#37;d\n", number);
    	number *= 2;
      }
    
      return 0;
    
    }
    Thank you in advance!!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you mean "doesn't work".

    If you put a "getchar()" inside your loop, perhaps you'll see it print 2, 4, 8, 16, 32, etc. But eventually the single 1-bit [all these numbers have only one bit set to one in them, it moves to the right when you multiply by 2] will fall off the left edge of the number [after 31 iterations of the loop if you are using a reasonably modern OS and compiler], and it will have no bits set to 0 - which means that the entire number is zero -> 0 * 2 = 0 so you'll forever see zero.

    --
    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.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Well the first few lines do what you expect.
    Then arithmetic overflow takes over and you end up with zero.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Location
    Reggio Emilia, Italy
    Posts
    15
    doh it was so logic!

    the fact is that I've seen a version of a friend of mine that differ only for while(foo)

    Code:
    --- my_friend_version.c	2007-09-26 12:14:41.000000000 +0200
    +++ my_version.c	2007-09-26 12:14:25.000000000 +0200
    @@ -3,11 +3,12 @@
     int main()
     {
       int number = 2;
    -
    -  while (number)
    +  
    +  while (1)
     	 {
     		printf("&#37;d\n", number);
     		number *= 2;
     	 }
       return 0;
     }
    and the version with while(number) stops after overflow. So I thaught my one should do the same; but control flow exits while when buffer overflows.

    I have undestand

    thank you again

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, your friends version stops when it overflows. But beware that not all types of overflow will be easily detected this way. If you multiply by 3, 5, 7 or some other other number, you may end up with digits in the lower part of the number, and thus the number will always have some non-zero value.

    Let's make this an 8 bit number (as it would require quite a lot more typing for a 32-bit number, but the principle is the same. Number in() is the binary form of the result), number in [] is ACTUAL result before overflow is removed
    Code:
      2 * 7 =  14 [  14] (00001110)
     14 * 7 =  98 [  98] (01100010)
     98 * 7 = 174 [ 686] (10101110) 
    174 * 7 =  98 [1218] (01100010)
    As you can see, it gets back to 98 after a few loops, and thus will NEVER exit the loop.

    --
    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.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Another way to check for overflow is to use
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main()
    {
    
      int number = 2;
    
      while (INT_MAX / number >= 2)
      {
    	printf("&#37;d\n", number);
    	number *= 2;
      }
    
      return 0;
    
    }
    INT_MAX is the maximum value that can be stored into an int. It's located in <limits.h>. There's also UINT_MAX (maximum value of an unsigned int), INT_MIN, LONG_MAX, and many other values in that header file.

    Of course, I haven't tested that code, but it should work.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Location
    Reggio Emilia, Italy
    Posts
    15
    well, since "we" are programmers (it would be better to say "you...")

    Code:
    #include <stdio.h>
    
    int main()
    {
     
      int counter = 1;
    
      while (counter++ <= 1000)
        printf("thank you!\n");
    
      return 0;
    
    }
    bye, Luca
    Last edited by lbraglia; 09-27-2007 at 10:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 11:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 05:36 PM

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