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