1

I can't understand why the following code outputs 10. What I understand is that !printf("0") means !0, which is TRUE. So why doesn't the code print "Sachin"

#include <stdio.h>

int main() {
    for (printf("1"); !printf("0"); printf("2"))
        printf("Sachin");
    return 0;
}

Output

10
4
  • 2
    Perhaps you need a printf reference to see what it returns? Commented Sep 11, 2018 at 7:53
  • 2
    printf returns the number of characters printed. It can print 1, 10 or nothing at all, depending on after which character it does fail. Commented Sep 11, 2018 at 7:54
  • 1
    !printf("0") returns false. printf itself returns the number of chars printed if successful. check any c reference for details. Commented Sep 11, 2018 at 7:58
  • !printf("0") means !0... No. Given the result of printf it means !1 which is FALSE Commented Sep 11, 2018 at 8:37

3 Answers 3

8

let's analyze this side-effect loop statement:

for(printf("1"); !printf("0"); printf("2"))
  • The first statement is executed, always (init condition), yieiding 1
  • Then the condition is tested: !printf("0") prints 0, then since printf returns 1 because it just prints 1 character, the negation returns 0 and the loop is never entered because the condition is false right from the start. So neither 2 or Sachin are printed.

Of course, this code isn't practical, almost unreadable. So don't ever do things like this (puts("10"); is a good alternative for instance).

more on the return value of printf (that is often ignored):

Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings).

(from https://linux.die.net/man/3/printf)

Sign up to request clarification or add additional context in comments.

4 Comments

Meaning no offence sir, this code may not be practical but to me it's experimental which is important to understand the things better. However, I thank you for your assistance. 😊
no offence taken. I added that bit in my answer so everyone understands that if you want to print 10 you just do printf("10");
And only write puts("10"); if you also require a newline. ;-)
aaah right I wanted to avoid printf when no formatting was needed :)
3

If you look at the man printf reference on google, you'll see that this function returns the number of written bytes.

Here your condition is !printf("0"), in other words : "as long as the return of printf is not existing (or equal 0), do something. But you print the character '0' so printf actually return 1 so your condition is false.

Now WHY it prints 10 :

  • The first printf("1") prints 1.
  • Your condition is tested at least once, so the second printf("0") occurs one time (it prints 0)

2 Comments

good first answer. Small piece of advice: don't tell people to "look on google". Google for them and include the link. It's better.
Thanks for the assistance.
0
printf("1")

prints 1 and it return number of characters which is 1

printf("0")

prints 0 and it return number of characters which is 1

!1 means !(true) = false so execution will stop and you will see 10 as output.

1 Comment

Understood. Thanks for the assistance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.