! operator

This is a discussion on ! operator within the C Programming forums, part of the General Programming Boards category; Question Is it true that you should avoid using the ! (not operator) and if so why is this? Does ...

  1. #1
    Perverted unanimous's Avatar
    Join Date
    Oct 2001
    Posts
    336

    Post ! operator

    Question

    Is it true that you should avoid using the ! (not operator) and if so why is this? Does it just make it harder to read?
    Give me a bad reputation!!!

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    where did you hear this? in a book? i find that weird... it's very useful... maybe you're thinking of 'goto'...
    hasafraggin shizigishin oppashigger...

  3. #3
    Registered User EvenFlow's Avatar
    Join Date
    Oct 2001
    Posts
    422
    I somehow think you may have gotten your wires crossed. The ! (not) operator is one of the most commonly used operators in C.
    Ramble on...

  4. #4
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Maybe he was worried about situations like this .
    Is the expression !(i) guaranteed to be 1 , or to be any non zero value ? I am myself not sure , does anyone know what the standard has to say about situations like this ?
    Code:
    #include <stdio.h>
    int main(void)
    {
      int i,j;
      i=0;
      j= !(i);
      printf("%d\n",j);
      return 0;
    }

  5. #5
    Registered User EvenFlow's Avatar
    Join Date
    Oct 2001
    Posts
    422
    >>is the expression !(i) guaranteed to be 1 , or to be any non zero value ?

    Well after running that several times, it always returned 1. But (pardon my ignorance), when would you ever need to use a calculation like that? What purpose would it serve?
    Ramble on...

  6. #6
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Well , I checked out , !0 is guaranteed to be one ,so it doesnt matter .
    The result is always going to be 1 on any ansi C compiler

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi,
    I find the ! operator useful when used as a flag when certain conditions are tested for in your program....
    example
    Code:
    #define OK 1
    int main (void)
    {
       int flag = OK;
    
       while(flag)
       {
        /*your program statments*/
        if(condition != expected_condition)
                 flag = !OK;
       } /*end while  */
    }
    Last edited by bigtamscot; 10-30-2001 at 01:14 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > Does it just make it harder to read?
    It is one of the smaller characters - so something like !i might be harder to read (but see how pinko_liberal solves it)

    > Is the expression !(i) guaranteed to be 1
    All the boolean and relational operators return either 0 or 1 - if you assign them to a value.

    Code:
    #include <stdio.h>
    
    int main ( ) {
        int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
        int i, done = 0;
        for ( i = 0 ; i < 10 && !done ; i++ ) {
            done = arr[i] == 4;  // a relational test and assignment
        }
        if ( done ) {
            printf( "Found at index %d\n", i-1 );
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Sayeh
    Guest
    Actually, the result is _not_ guaranteed to be one. It is guaranteed to be _not zero_. That may be negative or positive of any value other than zero.

    This is a logical (boolean) operator and is very standard because that is exactly how all tests are done in assembly language. ultimately EVERYTHING is tested against zero, because _zero_ is the only number you know about.

    Is it < 0?
    Is it > 0?
    Is it = 0?
    Is it ! 0?

    For example? Just how does a processor compare two numbers to see if they are equal? Well, it essentially subtracts one from the other and checks to see if the result is zero.

    As such, ! (not) is a very important operator.

    enjooy.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    I do wish people would read the standard....

    6.5.8 Relational operators

    Each of the operators < (less than), > (greater than), <= (less than or equal to), and >=
    (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false.)
    The result has type int.

    6.5.9 Equality operators
    The == (equal to) and != (not equal to) operators are analogous to the relational
    operators except for their lower precedence.81) Each of the operators yields 1 if the
    specified relation is true and 0 if it is false. The result has type int. For any pair of
    operands, exactly one of the relations is true.

    6.5.13 Logical AND operator
    The && operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it
    yields 0. The result has type int.

    Shall I go on?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    In C# the bool type is just true or false, not 1 or 0.

  12. #12
    BARJOR
    Guest
    There is nothing wrong with using the ! but sometimes when you have if(!cdr<=UYR && !tre == !fds,,,and so on) this can be kinda cumbersome to read so preferably you should try to rewrite this to something using less !.

  13. #13
    Barjor
    Guest
    There actually is a whole set of rules for how to convert || to && how to cancel out ! and so on, just like solving equations.

  14. #14
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Sayeh
    Actually, the result is _not_ guaranteed to be one. It is guaranteed to be _not zero_. That may be negative or positive of any value other than zero.

    This is a logical (boolean) operator and is very standard because that is exactly how all tests are done in assembly language. ultimately EVERYTHING is tested against zero, because _zero_ is the only number you know about.

    Is it < 0?
    Is it > 0?
    Is it = 0?
    Is it ! 0?

    For example? Just how does a processor compare two numbers to see if they are equal? Well, it essentially subtracts one from the other and checks to see if the result is zero.

    As such, ! (not) is a very important operator.

    enjooy.

    no !0 is guaranteed to be 1
    read the standard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21