Thread: Confusion with usage of ! operator in if statment

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    San Jose, California, United States
    Posts
    22

    Confusion with usage of ! operator in if statment

    See example below:

    Code:
    #include <stdio.h>
    #include <setjmp.h>
    
    static jmp_buf buf;
    
    void second(void) {
        printf("second\n");         // prints
        longjmp(buf,1);             // jumps back to where setjmp was called - making setjmp now return 1
    }
    
    void first(void) {
        second();
        printf("first\n");          // does not print
    }
    
    int main() {   
        if ( ! setjmp(buf) ) {
            first();                // when executed, setjmp returns 0
        } else {                    // when longjmp jumps back, setjmp returns 1
            printf("main\n");       // prints
        } 
        return 0;
    }
    I don't understand this part "if ( ! setjmp(buf) )" does. Can anyone explain?
    Last edited by vxs8122; 03-05-2013 at 01:40 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Place the code in code tags like this [code]/*YOUR CODE*/ [/code]
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    NOT (!) just flips Boolean results. Try reading the relevent section of this page.
    If Statements in C++ - Cprogramming.com

  4. #4
    Registered User
    Join Date
    Feb 2013
    Location
    San Jose, California, United States
    Posts
    22
    Then that leads to other question. I don't understand the sythax of this part. It sounds like an uncompleted sentence "If not setjmp(buf), then run function first()."

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well to fully understand the condition, you need to understand what setjmp returns and why. Then calling first when setjmp returns 0 should make sense.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Well we normally write what you have there, when the result is zero.

    Example.
    Code:
    int a = 0;
    if( a == 0)
    {
         //it's zero
    }
    else
    {
         // it's not zero
    }
    Many people write this code we just read like this (which is equivalent)
    Code:
    int a = 0;
    if( !a )
    {
         //it's zero
    }
    else
    {
         // it's not zero
    }
    a is zero, so we have as condition of if this: !0 which is 1 which results to true.

    It's nice to use this style when you have variables names found, etc. ...

    But notice that you have a .h file that defines what the function set returns.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Feb 2013
    Location
    San Jose, California, United States
    Posts
    22
    I see! The numbers are being treated as boolean TRUE and FALSE. Thanks for clearing up.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Remember: 0 results to false, anything else in true.

    You are welcome
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    22
    yes that menas if setjmp(buf) returns 0, then the if block will execute, saying this the setjmp() must be a function returning some kind of integer value..

  10. #10
    Registered User
    Join Date
    Feb 2013
    Location
    San Jose, California, United States
    Posts
    22
    Other question: does longjmp() cause a frame in the middle of the stack to be deallocated?

    For example:

    Code:
    #include <stdio.h>
    #include <setjmp.h>
     
    static jmp_buf buf;
     
    void second(void) {
        longjmp(buf,1);    
        printf("second\n");                     
    
    }
     
    void first(void) {
        second();
        setjmp(buf);
        printf("first\n");          
    
    }
     
    int main() {   
        first();
        return 0;
    }

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    This is something that had to do with the implementation of the function. This is something of the standard. You created setjmp.h or is it something that everybody around you use it?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    Registered User
    Join Date
    Feb 2013
    Location
    San Jose, California, United States
    Posts
    22
    No I did not create setjmp.h, it is already included in the library. Check out setjmp.h - Wikipedia, the free encyclopedia.

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by vxs8122 View Post
    Other question: does longjmp() cause a frame in the middle of the stack to be deallocated?
    The reason why your example doesn't work is that you call longjmp() before you call setjmp() thus the value of "buf" is garbage.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Problem About Usage of Bitwise Operator &
    By hefese in forum C Programming
    Replies: 5
    Last Post: 08-16-2012, 04:53 PM
  2. Operator >> confusion
    By sigur47 in forum C++ Programming
    Replies: 2
    Last Post: 02-25-2012, 04:24 AM
  3. confusion in EOF usage....
    By xterminator in forum C Programming
    Replies: 4
    Last Post: 04-01-2011, 10:47 PM
  4. && and || operator confusion
    By rohit_second in forum C Programming
    Replies: 8
    Last Post: 09-02-2008, 12:10 AM
  5. Proper Usage of the delete Operator
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2007, 11:53 PM