home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16897 < prev    next >
Encoding:
Internet Message Format  |  1992-11-20  |  1.5 KB

  1. Path: sparky!uunet!mcsun!sun4nl!and!jos
  2. From: jos@and.nl (Jos Horsmeier)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: C for loop
  5. Message-ID: <3898@dozo.and.nl>
  6. Date: 20 Nov 92 18:12:34 GMT
  7. References: <Bxzonp.2oH@research.canon.oz.au> <3892@dozo.and.nl> <1992Nov20.134343.4978@magnus.acs.ohio-state.edu>
  8. Organization: AND Software BV Rotterdam
  9. Lines: 38
  10.  
  11. In article <1992Nov20.134343.4978@magnus.acs.ohio-state.edu> ckkoo@magnus.acs.ohio-state.edu (Chee K Koo) writes:
  12. |I am new to C programming, while i was doing a numerical analysis
  13. |program which uses many for loop
  14. |here is the loop looks like
  15. |
  16. |for(i=1;i<=n;i++);
  17. |{
  18. |}
  19. |
  20. |i put the ";" after the loop header, when the program is excuted, the loop
  21. |is skipped, however, it works fine when the ";" is removed.
  22. |
  23. |I just want to find out why the compiler does not catch it during
  24. |compilation. It is compiled with UNIX gcc.
  25.  
  26. The compiler can't complain, because your code is perfectly legal 
  27. (syntactically speaking that is.) What the compiler actually `sees'
  28. is this: `for(i=1;i<=n;i++)<<empty statement expression>>;' followed
  29. by a block of code. The <<empty statement expression>> is allowed in C.
  30. In this situation it might seem silly to allow this, but have a look 
  31. at a naive example implementation of strlen:
  32.  
  33. size_t strlen(char *s) {
  34.  
  35.     size_t len;
  36.  
  37.     for (len= 0; *s; len++, s++);
  38.  
  39.     return len;
  40.  
  41. }
  42.  
  43. See? All the necessary actions are done _within_ the for loop part, i.e.
  44. no other statements are necessary here. Isn't C wonderful! ;-)
  45.  
  46. kind regards,
  47.  
  48. Jos aka jos@and.nl
  49.