Is it a valid statement - C language?

  • Thread starter Thread starter pairofstrings
  • Start date Start date
  • Tags Tags
    C language Language
Click For Summary

Discussion Overview

The discussion revolves around the interpretation of specific syntax in the C programming language, particularly focusing on the use of double exclamation marks and double percentage signs in code snippets. Participants explore the implications of these symbols, their meanings, and related concepts in C, while also addressing potential misunderstandings and clarifying syntax rules.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • Some participants suggest that the double exclamation marks represent logical negation, while others clarify that the presence of parentheses alters the interpretation.
  • There is uncertainty about the validity of using double percentage signs, with some participants asserting that they do not make sense in expressions.
  • One participant proposes that the code might be intended to use the equality operator (==) instead of the assignment operator (=), raising questions about the code's compilation.
  • Several participants discuss the precedence of logical operators and how they affect the evaluation of expressions, particularly in the context of boolean logic.
  • There is a request for clarification on how to interpret expressions involving logical AND (&&) and how they relate to the output of printf statements.
  • Some participants express frustration over the lack of clarity in the original question and the difficulty in analyzing the code without a complete understanding of its context.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the interpretation of the double percentage signs or the exact nature of the original code snippet. Multiple competing views remain regarding the correct syntax and its implications in C programming.

Contextual Notes

There are unresolved questions about the specific syntax used in the code, including the potential for typographical errors and the implications of using assignment versus comparison operators. Additionally, the discussion highlights the importance of understanding operator precedence in boolean expressions.

Who May Find This Useful

This discussion may be useful for individuals learning C programming, particularly those interested in understanding logical operators, syntax rules, and debugging code snippets.

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:
Technology 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%.
 
Your if statement might have been something like:

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

Any idea what that would do?
 
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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K