home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.amiga.programmer
- Path: sparky!uunet!ornl!utkcs2!darwin.sura.net!spool.mu.edu!agate!stanford.edu!rock!concert!sas!mozart.unx.sas.com!walker
- From: walker@twix.unx.sas.com (Doug Walker)
- Subject: Re: SAS6.1 users: please read
- Originator: walker@twix.unx.sas.com
- Sender: news@unx.sas.com (Noter of Newsworthy Events)
- Message-ID: <Bz3z3A.13E@unx.sas.com>
- Date: Fri, 11 Dec 1992 18:52:22 GMT
- References: <ByM64K.6z2@fc.hp.com>
- Nntp-Posting-Host: twix.unx.sas.com
- Organization: SAS Institute Inc.
- Lines: 73
-
-
- In article <ByM64K.6z2@fc.hp.com>, koren@fc.hp.com (Steve Koren) writes:
- |> Consider this code:
- |>
- |> /***********************************************************************
- |> ** sc test1.c
- |> ** slink from lib:c.o test1.o to test1 lib lib:sc.lib
- |> ***********************************************************************/
- |>
- |> # include <stdio.h>
- |> # include <string.h>
- |>
- |> main(int argc, char **argv) {
- |> char *str = "abc";
- |> int num = 7;
- |>
- |> if ((strlen(str) - num) > 0) {
- |> printf("Ooops!\n");
- |> }
- |>
- |> exit(0);
- |> }
- |>
- |> Even though one would expect
- |> if ((strlen(str) - num) > 0) {
- |>
- |> in cases where strlen(str)=3 and num=7 to fail (ie, 3-7 is -4,
- |> and -4 is NOT greater than 0 last time I checked), the if
- |> statement passes instead.
-
- This is not a bug, this is correct behavior. It's a confusing
- point in the ANSI C standard that I've explained here a couple of
- times before, in slightly different terms.
-
- strlen() is defined by the standard as returning a value of type
- "size_t", which must be an unsigned integer value. In our case,
- it is "unsigned int".
-
- The variable "num" is of type "int", of course, as is the constant
- zero. Therefore, the subtraction in the condition of the if
- statement is
-
- "unsigned int" - "int"
-
- The ANSI expression rules state that the types of the expression
- must be promoted to the "widest" type in the expression, or "int",
- whichever is smaller. i.e. in the above expression, the second
- "int" gets converted to "unsigned int" and the result of the
- expression is "unsigned int".
-
- Since the result of the expression is unsigned, it is impossible
- for the condition to be negative. Thus, the subtraction results
- in an underflow and the result is a large positive number.
-
- |> I hope this helps someone else avoid the code problems I was
- |> having. I'm sure they'll fix this for 6.2. SASC continues,
- |> IMHO, to be the best development environment for the Amiga bar
- |> none.
- |>
- |> - steve
-
- Thanks for the compliment. Unfortunately, we can't change this
- without violating the ANSI (and now ISO) standard for C expressions.
-
- --
- *****
- =*|_o_o|\\=====Doug Walker, Software Distiller====== BBS: (919)460-7430 =
- *|. o.| || 1200/2400/9600 Dual
- | o |// For all you do, this bug's for you!
- ======
- usenet: walker@unx.sas.com bix: djwalker
- Any opinions expressed are mine, not those of SAS Institute, Inc.
-
-