#include<stdio.h>
int main()
{
    char ch;
    if(ch = printf(""))
        printf("It matters\n");
    else
        printf("It doesn't matters\n");
    return 0;
}

Here according to me,
the expression inside "if" will be true becoz we have used "=" sign and not "==" sign.
but the output is "It doesn't matters".how is it so?

Anuradha Mandal commented: printf has nothing to print! +0

printf returns the number of chars printed, and you didn't print any objects, so it would return a negative number, as an error IF your compiler allowed it to compile.

Perhaps you were thinking of:

if((ch = printf(something here)) > 0)
  //semi-colon or other code goes here

That's the normal idiom for this kind of if statement, IF I understand what you want, correctly.

printf returns the number of chars printed, and you didn't print any objects, so it would return a negative number, as an error IF your compiler allowed it to compile.

printf() only returns a negative value if there's an "output or encoding error". Since printf()'s internal loop is essentially skipped when printing an empty format string, neither of those will happen:

int printf(const char *s, ...)
{
    int n_char = 0;

    while (*s != '\0') {
        ...
    }

    return n_char;
}

Thus the return value for printf("") will be 0, and that falls into the else clause of the OP's main().

@Narue
Please check whether the sentences given below are correct or not
printf()
returns 0 when nothing is printed.
returns negative value if there is some output error.
But what are these output error?

But what are these output error?

If characters can't be written to the output device for any reason. For example, if you're writing to a file on a USB device and uplug it.

if(ch = printf(""))

I dont understand you condition.
And If is not a loop.

printf returns number of character inside the double quote. since there is nothing inside the quote so 0 is assigned to ch and so condition is false and the compiler go to else condition result in printing it doesn't matter.

printf returns number of character inside the double quote

So printf("%d", 12345) returns 2? ;)

So printf("%d", 12345) returns 2?

no it will return 5

I was talking about printf("12345");
actually printf function always returns the number of characters printed.

no it will return 5

I was talking about printf("12345");
actually printf function always returns the number of characters printed.

My subtle point is that your original statement was misleading.

I will surely keep these things in my mind not to mislead before typing any solutions.

Why you use this..

if(ch = printf(""))

printf only uses for printing something.
You may be miss something.

Why you use this..

printf only uses for printing something.
You may be miss something.

You're the one who's missing something. Since you don't appear to know what you're talking about in the majority of your posts, might I suggest asking questions rather than trying to answer them?

You're the one who's missing something.

Sorry if I do wrong .I am not sure about the last post.But can you say why printf is used in the actual post? How it works?

But can you say why printf is used in the actual post? How it works?

Have you tried reading this thread? It has already been mentioned that printf() returns the number of characters written. As for why the OP wrote that code, I'm not sure. It's clearly a test of something, probably a combination of printf()'s return value and the behavior of a if statement condition without any explicit relational operators.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.