Thread: what does !c mean?

  1. #16
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I stand corrected and feel somewhat enlightened...but only somewhat, since I don't quite get all your math speak. Oh well, it makes sense. Do you happen to have a link to the C++ thread? I'd love to read it, but a search for "predetermined integers" gave me 2 irrelevant threads, and I don't quite feel like sifting through laserlight's and Elysia's combined 30,000+ posts. Thanks.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    Because !c will first invoke operator ! on c, then compare to true, while if (c == 0) will compare c to 0, then compare to true.
    So they are two different operations with the same result in the end.
    That does not matter as the operations are equivalent (and the equivalence is explicitly stated in C99, though in the (0 == c) form). That is b1nd3r's point, so there is no point nitpicking, unless you want to insist that *p is not "equal" (as in equivalent) to p[0], since they are two different operations with the same result in the end.

    Quote Originally Posted by anduril462
    Do you happen to have a link to the C++ thread? I'd love to read it, but a search for "predetermined integers" gave me 2 irrelevant threads, and I don't quite feel like sifting through laserlight's and Elysia's combined 30,000+ posts.
    Heheh, I don't have the link handy either, but...
    Quote Originally Posted by anduril462
    The ! operator works on integer expressions, and does one of two things. If the expression is non-zero, it makes it zero. If the expression is zero, it makes it non-zero. It doesn't say what kind of non-zero value, it's not some specific "true" value or 1 of 0xFFFFFFFF, not necessarily at least.
    It does say what kind of non-zero value: the result is of the type int with a value of 1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by laserlight View Post
    That does not matter as the operations are equivalent (and the equivalence is explicitly stated in C99, though in the (0 == c) form). That is b1nd3r's point, so there is no point nitpicking, unless you want to insist that *p is not "equal" (as in equivalent) to p[0], since they are two different operations with the same result in the end.
    Yeah that's what I was trying to say as well, I guess you just have a gift of putting thoughts into words better, which would explain your gazillion posts .
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #19
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Anduril462,

    The link you requested: Inputting a dynamic, mutable character array with spaces

    The discussion was about how silly it is to use an integer type for a pointer value (I think it escalated to an evil eventually) ...a funny symmetry to this discussion -- I thought at the time.

    Best Regards,

    New Ink -- Henry
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

  5. #20
    Registered User
    Join Date
    Sep 2010
    Posts
    25

    "!" is the "not" that turns true into false and false into true.

    K&R's C, Chapter 2, Section 6.

    "The unary negation operator ! converts a non-zero operand into 0, and a zero operand into 1. A common use of ! is in constructions like
    Code:
    if (!valid)
    rather than
    Code:
    if (valid == 0)
    It is hard to generalize which form is better."

    (Kernighan and Ritchie 42).

    It reads as "not", but can also mean "empty." More closely, it can read, "opposite." [Difference between zero and empty; we'll spare you the semantics as you've probably already thought about it some.]

    Increments built up from zero are easy to imagine; but, sometimes the shorter path is to understanding what a condition is not; then, from there, building out the response. Sometimes very handy for those moments when you're going back and forth over a variable used as a trigger, without wanting to completely reset everything to the concept of "zero"; instead, you can employ the "!" as part of a procedure for checking for a change in state or changing a state. From there, continue programming.

    For this reason, it can be handy to invert the presence of electricity into its absence, or absence into presence. So, there is an operator for that concept, it's the "!".

    True into false; false into true. Good luck.
    Last edited by agxphoto; 11-25-2010 at 09:42 AM.

  6. #21
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Why do people keep insisting on quoting K&R which is like 30-something years old now? If you're going to quote something, quote the C99 standard. Which, by the way, has this to say:

    "The result of the logical negation operator ! is 0 if the value of its operand compares
    unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int.
    The expression !E is equivalent to (0==E)."
    Last edited by itsme86; 11-25-2010 at 10:30 AM.
    If you understand what you're doing, you're not learning anything.

  7. #22
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    Well, if I didn't quote K&R's C, then you might have mistaken all of those great ideas for my own!
    Last edited by agxphoto; 11-25-2010 at 11:48 AM.

Popular pages Recent additions subscribe to a feed