Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

printf returns 6 instead of 0 when assigning a null string

Why does printf function return 6 instead of 0 when assigning NULL as string to it, although its printing nothing?

int main(void)
{
    int x;
    x = printf("%s", NULL);
    printf ("\n%d", x);
}

output is:

(null)
6

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

why does printf function return 6 instead of 0 when assigning NULL as string to it

Because it printed 6 chars (null) is 6 chars.

although its printing nothing?

It is printing (null). That’s 6 chars.

Note that passing NULL as an argument to %s format specifier is invalid in the first place and causes undefined behavior. Some implementations, like the one you are using, are printing (null) instead of crashing.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading