Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Is it a valid statement - C language?

  1. Aug 15, 2011 #1
    Here is a part of a statement, it might be incorrect, I don't remember the exact statement. Please tell me what the double exclamation marks means and what about the double percentages.

    Here is the code.

    if (!(!d = %%d))
    else ... ( not spot-on, don't remember)


    I think of this as miscellaneous material in C language. Is there any other miscellaneous stuff like this in 'C' that I should know?
    Where can I find such material in written form, so that I could read about it and gain knowledge?

    Thank you very much.
     
    Last edited: Aug 15, 2011
  2. jcsd
  3. Aug 15, 2011 #2

    DaveC426913

    User Avatar
    Gold Member

    ! means not.

    That's not a double !, there's a parenthesis in there, which changes it completely.

    % means modulo; I am not sure if %% is valid.
     
  4. Aug 15, 2011 #3
    Can you please tell me what it means if there are two !! together and when they are separated by parenthesis. Replace %% with && now? What does it mean?
     
  5. Aug 15, 2011 #4

    DaveC426913

    User Avatar
    Gold Member

    This sounds like homework. You're supposed to parse the statement and figure out the result.

    ! negates a boolean. So !! restores it.

    !true = false
    !false = true
    !(!true) = true
    !(!false) = false

    But if there are parentheses then you must process them in order of precedence (just like regular arithmetic, where you process x and / before + and -).
     
  6. Aug 15, 2011 #5
    No, it's a question from the test that I wrote yesterday. I don't remember the question correctly. But I want to learn this stuff.
     
  7. Aug 15, 2011 #6

    DaveC426913

    User Avatar
    Gold Member

    % is a formatter, forces a type (%d forces to integer).

    Is it possible there was a space? i.e. if (!(!d = %_%d)) No, that doesn't make sense either.

    How could you be doing a test on this and not know the basics? Did you just grab a test from somewhere and give it a try?
     
  8. Aug 15, 2011 #7

    I like Serena

    User Avatar
    Homework Helper

    Hi pairofstrings and Dave! :smile:

    Hope you don't mind me butting in. :shy:

    A percentage in an expression can only be the modulo operation.
    Two percentage signs in a row make no sense whatsoever.

    In a printf-like statement the percentage sign is indeed a formatter.
    So for instance printf("%d%%", 3) would print: 3%.
     
  9. Aug 15, 2011 #8

    I like Serena

    User Avatar
    Homework Helper

    Your if statement might have been something like:

    if (!(!d && d)) { printf("%d%%", d); }

    Any idea what that would do?
     
  10. Aug 15, 2011 #9

    Mark44

    Staff: Mentor

    Another thing with the code above is that it won't compile. The code uses = (assignment) when it probably was == (compare for equality). The reason it won't compile is that what's on the left side of the assignment operator is not an lvalue, an expression that represents a variable or memory location.
     
  11. Aug 15, 2011 #10
    No, I remember vividly. There was no space. I don't remember if there was "=" in if (!(!d = %_%d)).

    I wrote the test because I thought I knew enough about the basics. But it turned out that I was wrong. :(
     
  12. Aug 15, 2011 #11
    It could be == in place of =.
     
  13. Aug 15, 2011 #12
    If "d" is a variable then I think it should print it's value because of the double ampersand?
     
  14. Aug 15, 2011 #13

    I like Serena

    User Avatar
    Homework Helper

    First you need to know what the double ampersand means.
    It means "logical and".
    So for instance (true && false) evaluates as (false).
    Dave already explained that "!" is the logical not.

    Now suppose d is a boolean with say the value "true".
    What do you think the expression evaluates to?
     
  15. Aug 15, 2011 #14
    Evaluates as : true3 ?

    I think it's "true3" because there are three % in printf("%d%%", d); }

    It cannot be "truetruetrue" because there is one %d and no d's for the remaining two ampersands.
     
    Last edited: Aug 15, 2011
  16. Aug 15, 2011 #15

    I like Serena

    User Avatar
    Homework Helper

    Dave, Mark, errrr.... help?
     
  17. Aug 15, 2011 #16

    I like Serena

    User Avatar
    Homework Helper

    I'm taking a few steps back.
    Let's start with (!d && d).
    What will that evaluate to if d equals "true"?
     
  18. Aug 15, 2011 #17
    false && true evaluates to false then there is ! that means !false which evaluates to "true".
     
  19. Aug 15, 2011 #18

    I like Serena

    User Avatar
    Homework Helper

    Hold on, I don't get what you just did.

    We have (!d && d) with d equal to true.
    So that is: (!(true) && true).

    How did you get true && false?
     
  20. Aug 15, 2011 #19

    I like Serena

    User Avatar
    Homework Helper

    Hmm, I'm not sure what you get and what you don't get.
    I guess you did get what the if-expression evaluates to, which is "true'.

    All right, I'll just move on to the printf-statement.

    "%d" is a format specifier to show 1 integer.
    "%%" is a format specifier to show a percentage sign.
    In particular it does not show any number.

    If a boolean is shown as an integer, it is first converted to a number.
    "true" is converted to the number "1".
    So "%d" is evaluated as "1".
    And "%%" is evaluated as "%".

    The result is that "1%" is printed on the output.
    In particular, "true" is not printed on the output.

    I'm sorry if I was a bit condescending, but your result was so far off and you skipped so many steps, that I couldn't help it.
     
    Last edited: Aug 15, 2011
  21. Aug 15, 2011 #20

    Mark44

    Staff: Mentor

    It's hard to analyze what some code is doing if the OP (pairofstrings) doesn't remember the code all that well.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook




Loading...