Is it a valid statement - C language?

In summary: So in this case, !false would return (true).First you need to know what the double ampersand means.It means "logical and".So for instance (true && false) evaluates as (false).
  • #1
pairofstrings
411
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:
Technology news on Phys.org
  • #2
! 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
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?
 
  • #4
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 -).
 
  • #5
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.
 
  • #6
% 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
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%.
 
  • #8
Your if statement might have been something like:

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

Any idea what that would do?
 
  • #9
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.
 
  • #10
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. :(
 
  • #11
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 =.
 
  • #12
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?
 
  • #13
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?
 
  • #14
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:
  • #15
Dave, Mark, errrr... help?
 
  • #16
I'm taking a few steps back.
Let's start with (!d && d).
What will that evaluate to if d equals "true"?
 
  • #17
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".
 
  • #18
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?
 
  • #19
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:
  • #20
It's hard to analyze what some code is doing if the OP (pairofstrings) doesn't remember the code all that well.
 
  • #21
I don't know how to help. As far as I can determine, %%d is invalid.
 
  • #22
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?
 
  • #23
What happens if there are 2 ampersands or 2 exclamation marks in printf statement?

printf("%d&&",d) or printf("%d!",d)
 
  • #24
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!".
 
  • #25
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?
 
  • #26
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.
 

1. Is C language considered a valid programming language?

Yes, C language is considered a valid programming language. It is a high-level language that is widely used for developing operating systems, system software, and applications. It is also known for its efficiency, portability, and low-level memory access.

2. Can C language be used for web development?

Yes, C language can be used for web development, but it is not commonly used for this purpose. C language is more suited for low-level programming and is not as well-equipped for web development as other languages like HTML, CSS, and JavaScript.

3. What are the main features of C language?

The main features of C language include portability, efficiency, low-level memory access, and a rich library of built-in functions. It also allows for modular programming with its support for functions and data structures.

4. Is C language still relevant in today's programming landscape?

Yes, C language is still very relevant in today's programming landscape. It is widely used in industries like operating systems, embedded systems, and system programming. It is also the basis for many other programming languages, such as C++, Java, and Python.

5. Are there any drawbacks to using C language?

One of the main drawbacks of using C language is its steep learning curve for beginners. It also requires manual memory management, which can be challenging for some programmers. Additionally, C language does not have built-in support for certain modern programming concepts, such as object-oriented programming.

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
8
Views
867
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top