# Is it a valid statement - C language?

1. Aug 15, 2011

### pairofstrings

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. Aug 15, 2011

### DaveC426913

! 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.

3. Aug 15, 2011

### pairofstrings

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?

4. Aug 15, 2011

### DaveC426913

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 -).

5. Aug 15, 2011

### pairofstrings

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.

6. Aug 15, 2011

### DaveC426913

% 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?

7. Aug 15, 2011

### I like Serena

Hi pairofstrings and Dave!

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%.

8. Aug 15, 2011

### I like Serena

Your if statement might have been something like:

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

Any idea what that would do?

9. Aug 15, 2011

### 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.

10. Aug 15, 2011

### pairofstrings

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. :(

11. Aug 15, 2011

### pairofstrings

It could be == in place of =.

12. Aug 15, 2011

### pairofstrings

If "d" is a variable then I think it should print it's value because of the double ampersand?

13. Aug 15, 2011

### I like Serena

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?

14. Aug 15, 2011

### pairofstrings

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
15. Aug 15, 2011

### I like Serena

Dave, Mark, errrr.... help?

16. Aug 15, 2011

### I like Serena

I'm taking a few steps back.
What will that evaluate to if d equals "true"?

17. Aug 15, 2011

### pairofstrings

false && true evaluates to false then there is ! that means !false which evaluates to "true".

18. Aug 15, 2011

### I like Serena

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?

19. Aug 15, 2011

### I like Serena

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
20. Aug 15, 2011

### Staff: Mentor

It's hard to analyze what some code is doing if the OP (pairofstrings) doesn't remember the code all that well.