Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #10924

Re: A doubt about infinite loop

From Kleuskes & Moos <kleuske@somewhere.else.net>
Newsgroups comp.lang.c
Subject Re: A doubt about infinite loop
Date 2011-09-25 15:03 +0000
Organization A noiseless patient Spider
Message-ID <j5nfrv$8dp$1@dont-email.me> (permalink)
References <57b6390c-d172-46a1-8607-fa9d9a917617@k15g2000yqd.googlegroups.com>

Show all headers | View raw


On Sun, 25 Sep 2011 06:32:12 -0700, Anshum Kocher wrote:

> main()
> {
>   unsigned int i=10;
>   while(i-->=0)
>     printf("%u ",i);
> int a[10];
>   printf("%d",*a+1-*a+3);
> 
> 
> }
> This leads to an infinite loop

It should. Can an unsigned integer be negative? If not, why do
you expect that loop to be finite?

>  main()
> {
>   unsigned int i=10;
>   while(i-=1>=0)
>     printf("%u ",i);
> int a[10];
>   printf("%d",*a+1-*a+3);
> }
> 
> But this does not why?

In the second case, the condition isn't what you think it is. Suggestion:
Change "while(i-=1>=0)" to "while((i-=1)>=0)", thus forcing a particular
precedence and see what happens.

More advice: keep boolean expressions and assignments strictly separated,
avoid clever tricks like the plague and be copious with '(' and ')' if
you have _any_ doubts about operator precedence.

-------------------------------------------------------------------------------
 _____________________________________
/ They collapsed ... like nuns in the \
\ street ... they had no teen appeal! /
 -------------------------------------
  \
   \
       ___  
     {~._.~}
      ( Y )
     ()~*~()   
     (_)-(_)   
-------------------------------------------------------------------------------

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

A doubt about infinite loop Anshum Kocher <email.anshum@gmail.com> - 2011-09-25 06:32 -0700
  Re: A doubt about infinite loop Harald van Dijk <truedfx@gmail.com> - 2011-09-25 07:32 -0700
  Re: A doubt about infinite loop jt@toerring.de (Jens Thoms Toerring) - 2011-09-25 14:39 +0000
  Re: A doubt about infinite loop Kleuskes & Moos <kleuske@somewhere.else.net> - 2011-09-25 15:03 +0000
  Re: A doubt about infinite loop Seebs <usenet-nospam@seebs.net> - 2011-09-26 22:59 +0000
    Re: A doubt about infinite loop Mark Bluemel <mark_bluemel@pobox.com> - 2011-09-27 08:40 +0100
      Re: A doubt about infinite loop gazelle@shell.xmission.com (Kenny McCormack) - 2011-09-27 10:35 +0000
      Re: A doubt about infinite loop Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-09-27 14:05 +0300
        Re: A doubt about infinite loop gazelle@shell.xmission.com (Kenny McCormack) - 2011-09-27 12:35 +0000
        Re: A doubt about infinite loop Nobody <nobody@nowhere.com> - 2011-09-27 19:10 +0100
          Re: A doubt about infinite loop Seebs <usenet-nospam@seebs.net> - 2011-09-27 22:37 +0000
      Re: A doubt about infinite loop Seebs <usenet-nospam@seebs.net> - 2011-09-27 22:37 +0000

csiph-web