Re: [NEWBIE] question

From: Peter Ajamian (peter@pajamian.dhs.org)
Date: 04/03/01


Del wrote:
>
> Edward Felch wrote:
> >
> > I hate to ask a basic c question, but how does the ? in: GET_POS(ch) ==
> > POS_SITTING ? "sitting" : "resting"  work? Thanks a lot... ive been
> > wondering
> >
>
> I am no expert, but it works like this (correct me if I am wrong)
>
> if (GET_POS(ch) == POS_SITTING)
>      "sitting"
> else
>      "resting"

Close, but not quite.  The difference is that the former is an operator
which tests the first argument and returns either the second or the third
depending on the result.  The latter is a statement which tests the
expression and executes one of two code blocks depending on weather or
not it's true.  If that was too complex for you then I'll try to simplify
it with a couple of examples...

printf("%s\n", (1 ? "true" : "false"));

This is valid and will print "true", but you can't do this with the
statement, ie, the following is not legal C...

printf("%s\n", (if (1) "true"; else "false"));

So as you can see, the two are not interchangeable, likewise, you can do
the following...

if (1)
  printf("true\n");
else
  printf("false\n");

...but you cannot do this...

1 ?
  printf("true\n");
:
  printf("false\n");

So as you can can see, once again, the two are not interchangeable.

Regards, Peter

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/05/01 PST