Thread: ! unary operator

  1. #1
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105

    ! unary operator

    hi everyone.....I am having a confusion with the implementation of condition of a while loop expression.Suppose the code
    Code:
    while(!feof(fp))
    {
         printf("%c",ch);
         fscanf(fp,"%c",&ch);
    }
    Here although I have found out that that 0 means FALSE and 1 or any nonzero number means TRUE.
    So if the condition is 1 the loop executes and if the loop is 0 the loop terminates.
    So upto this much i could understand.
    Now as we know feof returns a nonzero no. if all the characters of a file has been read else it returns 0.
    So how its interpreted here by the compiler??
    I mean if it is feof() is 0 then it makes it nonzero using the ! operator but what about when feof() is nonzero, whether !(nonzero number)=0?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Everything zero becomes "not zero", which naturally evaluates to true. Everything "not zero" becomes zero, which evaluates to false.

    feof should be returning zero all the time, until it finally sees that the EOF has been reached, at which point it returns non-zero (which gets negated).


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    for me loop

    Code:
    while(fscanf(fp,"%c",&ch) == 1)
    {
         printf("%c",ch);
    }
    looks simplier
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by quzah View Post
    Everything zero becomes "not zero", which naturally evaluates to true. Everything "not zero" becomes zero, which evaluates to false.

    feof should be returning zero all the time, until it finally sees that the EOF has been reached, at which point it returns non-zero (which gets negated).


    Quzah.
    So you mean
    !(nonzero number)=0
    and
    !(zero)=nonzero number.......
    ok thats what i wanted to know
    Thanks alot

  5. #5
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by vart View Post
    for me loop

    Code:
    while(fscanf(fp,"%c",&ch) == 1)
    {
         printf("%c",ch);
    }
    looks simplier
    whether here fscanf() returns 1 till it gets a character and returns a zero when it gets the EOF.....is it like that???I am not able to get the idea

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by rakeshkool27 View Post
    whether here fscanf() returns 1 till it gets a character and returns a zero when it gets the EOF.....is it like that???I am not able to get the idea
    Have you read description of scanf?

    the loop is basically like that:

    Code:
    while(you read something from file)
    {
       do something with what you've read
    }
    fscanf returns number of tokens successfully parsed. In your case there is 1 token %c

    so while fscanf returns 1 - you have something read into ch

    Any other value will indicate that read failed (due to EOF condition or FAIL condition)...
    In this case ch will have incorrect value and should not be processed
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by vart View Post
    Have you read description of scanf?

    the loop is basically like that:

    Code:
    while(you read something from file)
    {
       do something with what you've read
    }
    fscanf returns number of tokens successfully parsed. In your case there is 1 token %c

    so while fscanf returns 1 - you have something read into ch

    Any other value will indicate that read failed (due to EOF condition or FAIL condition)...
    In this case ch will have incorrect value and should not be processed
    So incase there are more tokens as

    fscanf(fp,"%c %c %c",&ch1,&ch2,&ch3);

    then it should be

    while(fscanf(fp,"%c %c %c",&ch1,&ch2,&ch3) == 3)

    right nah????

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by quzah View Post
    Everything zero becomes "not zero", which naturally evaluates to true. Everything "not zero" becomes zero, which evaluates to false.
    Or, as per the C99 draft. The result of ! is 0 if its operand compares to 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).

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by rakeshkool27 View Post

    then it should be

    while(fscanf(fp,"%c %c %c",&ch1,&ch2,&ch3) == 3)
    yes, like that
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by vart View Post
    yes, like that
    ok got it..........

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM