home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!charon.amdahl.com!pacbell.com!att!linac!pacific.mps.ohio-state.edu!zaphod.mps.ohio-state.edu!uunet.ca!wildcan!sq!msb
- From: msb@sq.sq.com (Mark Brader)
- Subject: Re: Order of execution...
- Message-ID: <1992Aug30.204056.27873@sq.sq.com>
- Organization: SoftQuad Inc., Toronto, Canada
- References: <7942@lee.SEAS.UCLA.EDU>
- Distribution: usa
- Date: Sun, 30 Aug 92 20:40:56 GMT
- Lines: 54
-
- > while ((textline[index++] = tolower(textLine[index])) != '\0');
- >
- > The question is will it always work? Or will sometimes work depending
- > on which side of the assignment gets evaluated first...?
-
- It will never work until you spell your variables consistently, or use
- a macro to alias the two spellings!
-
- Ignoring that... the code will indeed only "sometimes work", but not
- because the compiler may evaluate one or the other side of the assignment
- first. It can actually interlace the evaluation of the two parts!
- Thus it can choose to begin by evaluating the index++ on the left and
- by storing the incremented result in the variable, and then continue
- by evaluating the right side, and then finish off the left side.
-
- > Is there a better way to do this (and still keep it a one liner).
-
- Actually, most people feel that writing it on one line as it is is a
- bad idea; the single semicolon that is the loop body is hard to see.
- There are various other styles; I like the next tersest, which is to put
- the semicolon alone on the next line.
-
- You want to get the increment out of the assignment, and the neatest
- way to do this is to use a for loop. I have also separated the test
- from the assignment, which in this case seems clearer to me and also
- avoids calling tolower() on the final null.
-
- for ( ; textLine[index] != '\0'; ++index)
- textLine[index] = tolower (textLine[index]);
-
- You may of course write this on one line if you wish. I'd probably
- eliminate the needless variable "index" and write it like this:
-
- for ( ; *s; ++s) *s = tolower (*s);
-
- or, some days, slightly more verbosely:
-
- for ( ; *s != '\0'; ++s)
- *s = tolower (*s);
-
- where "char *s;" replaces your "char textLine[];", on the grounds that
- (opinions follow) stereotypical uses of variables do not need long names,
- and that formal parameters should be declared with their true types.
-
- Incidentally, note that applying tolower() to characters which may not
- be upper case letters is NOT portable to pre-ANSI C implementations.
- For this article I am assuming that either the implementation is ANSI C
- or that the string has been verified to contain only upper case letters.
- --
- Mark Brader At any rate, C++ != C. Actually, the value of the
- SoftQuad Inc., Toronto expression "C++ != C" is implementation-defined.
- utzoo!sq!msb, msb@sq.com -- Peter da Silva
-
- Actually that value is undefined. This article is in the public domain.
-