Is it a valid statement - C language?

  • Thread starter Thread starter pairofstrings
  • Start date Start date
  • Tags Tags
    C language Language
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
25 replies · 6K views
pairofstrings
Messages
411
Reaction score
7
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:
Physics news on Phys.org
! 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.
 
DaveC426913 said:
That's not a double !, there's a parenthesis in there, which changes it completely.

% means modulo; I am not sure if %% is valid.

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?
 
pairofstrings said:
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?

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 -).
 
DaveC426913 said:
This sounds like homework. You're supposed to parse the statement and figure out the result.

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.
 
% 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?
 
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%.
 
pairofstrings said:
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?

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.
 
DaveC426913 said:
% 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?

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

It could be == in place of =.
 
I like Serena said:
Your if statement might have been something like:

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

Any idea what that would do?

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

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?
 
I like Serena said:
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?

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:
I like Serena said:
I'm taking a few steps back.
Let's start with (!d && d).
What will that evaluate to if d equals "true"?

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

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?
 
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:
I don't know how to help. As far as I can determine, %%d is invalid.
 
I like Serena said:
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?
I edited that post and made it to False && True

I know that is (!(true) && true) which evaluates to false && true because there is ! before (true). Here, I gave precedence to ! and not &&, because it's with parenthesis.
Then the final calculation will be (!(!d && d)= !(false && true) = !(false) = true = 1. Correct?
 
What happens if there are 2 ampersands or 2 exclamation marks in printf statement?

printf("%d&&",d) or printf("%d!",d)
 
pairofstrings said:
I edited that post and made it to False && True

I know that is (!(true) && true) which evaluates to false && true because there is ! before (true). Here, I gave precedence to ! and not &&, because it's with parenthesis.
Then the final calculation will be (!(!d && d)= !(false && true) = !(false) = true = 1. Correct?


Correct! :approve:

Your grasp of boolean expressions and of order of precedence appears to be in order.

Now what would happen if d were for instance an integer with the value -1?
And what would happen if d were the number 0?


pairofstrings said:
What happens if there are 2 ampersands or 2 exclamation marks in printf statement?

printf("%d&&",d) or printf("%d!",d)


An ampersand and an exclamation mark in a printf statement have no particular meaning.
This means they are simply printed on the output as is.

In your case the output will be "1&&" respectively "1!".
 
I like Serena said:
Now what would happen if d were for instance an integer with the value -1?
And what would happen if d were the number 0?

!(!(d) && d) = !(!(-1) && -1) = !( 1 && 1) = !(1) = 0
!(!(d) && d) = !(!(0) && 0) = !( 1 && 0) = !(0) = 1

Wrong?
 
pairofstrings said:
!(!(d) && d) = !(!(-1) && -1) = !( 1 && 1) = !(1) = 0
!(!(d) && d) = !(!(0) && 0) = !( 1 && 0) = !(0) = 1

Wrong?

Yep. Wrong.

What you need to know is that 0 is treated as false, while any number that is not zero (like -1) is treated as true.