[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Please don't do this (code fragment)



>>"Olaf" == Olaf Weber <olaf@infovore.xs4all.nl> writes:

 Olaf> Going back to the original code, the assumption that

 Olaf>         int i;
 Olaf>         for (i = 0; i > 0; ++i)
 Olaf>                 ;

 Olaf> will terminate, and will terminate within a reasonable amount of time,
 Olaf> is not warranted. 

	Uhh? Am I missing something? The code, as written, terminates
 immediately, without entering the loop. A for loop in C sets the
 initial condition, and *TESTS AT THE TOP*.  Since i is initially set
 to 0, the test fails immediately. 

	I have indeed tested this.

	manoj
======================================================================
#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
  int i = 0;
  for(i = 0; i > 0; i++)
   {
     printf("i=%d\n", i);
   } /* end of for(i = 0; i > 0; i++) */
  exit (EXIT_SUCCESS);
} /* end of main() */
========================================================================
__> gcc -ansi -pedantic -Wall -W -Wtraditional -Wconversion -Wshadow \
         -Wpointer-arith -Wbad-function-cast -Wcast-qual -Wcast-align \
         -Wwrite-strings -Waggregate-return -Wstrict-prototypes \
         -Wmissing-prototypes -Wmissing-declarations \
         -fshort-enums -fno-common -Dgets=DONT_USE_GETS -Dlint \
         -Woverloaded-virtual -Wnested-externs -Winline -o testloop \
         -O2 testloop.c
__> ./testloop 
__>

-- 
 In this world, truth can wait; she's used to it.
Manoj Srivastava   <srivasta@debian.org>  <http://www.debian.org/%7Esrivasta/>
1024R/C7261095 print CB D9 F4 12 68 07 E4 05  CC 2D 27 12 1D F5 E8 6E
1024D/BF24424C print 4966 F272 D093 B493 410B  924B 21BA DABB BF24 424C



Reply to: