Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

shafana_123

New Coder
Hello, I'm fairly new, and I was wondering, why does

C:
!(printf("Hello world"));

still print "Hello world? Even though it's using the logical NOT operator...
As well as if it was used as a condition in a while() loop...

C:
while(!(printf("Hello world"))) {}

Why does it print only once? I thought the while() loop required the condition to be True, in order to print. I kind of understand why it prints "Hello world" infinitely without the !.
Thank you
 
Hello there,

Could you please point out where you've seen code like this used? I don't really have a clue why anybody would write a simple print statement out like that, unless they're making a conditional test that involves a print statement.

From what I can see(from the K&R C book), any brackets(() []) come first in order of precedence. The logical operator, ! follows it after. So, the compiler would examine the code by reading what's in the brackets first and print the statement that's inside.

I have tried putting the statement into an if-statement, however, it still prints. So, aside from the order of precedence, the string argument that printf takes must automatically turn the return value to true for it to print, despite the NOT operator being there. This is a funny one to try and wrap your head around.

Again, some examples of where you've seen this would be great because then we can examine it in action.
 
Yes, this is strange coding indeed. What you are doing is testing the return value of printf(), which is mighty uninteresting. And to test the return value, the statement must be executed first ! Do not assume that the exclamation mark means "do not execute the following statement".
The loop is even more contrived. You understand why it prints indefinitely without the exclamation mark, which is good. But why are you surprised it prints "only once" with the exclamation mark ? How many times would you have expected ?
 
Hello there,

Could you please point out where you've seen code like this used? I don't really have a clue why anybody would write a simple print statement out like that, unless they're making a conditional test that involves a print statement.

From what I can see(from the K&R C book), any brackets(() []) come first in order of precedence. The logical operator, ! follows it after. So, the compiler would examine the code by reading what's in the brackets first and print the statement that's inside.

I have tried putting the statement into an if-statement, however, it still prints. So, aside from the order of precedence, the string argument that printf takes must automatically turn the return value to true for it to print, despite the NOT operator being there. This is a funny one to try and wrap your head around.

Again, some examples of where you've seen this would be great because then we can examine it in action.
i totally agree with this.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom