2

I tried the following code on Linux

#include<stdio.h>
int main()
{
    char *p=NULL;
    printf("%s",p);
    return 0;
}

#include<stdio.h>
int main()
{
    char *p=NULL;
    printf("%s\n",p);
    return 0;
}

The first one outputs: (null)

While the second one causes a segmentation fault.

Why does \n make such a difference?

user4020527
  • 1
  • 8
  • 20
Golden Fish
  • 81
  • 1
  • 9

1 Answers1

0

Both of your examples are undefined behavior per standard. Calling printf with %s and passing a NULL pointer is UB.

Therefore it makes no sense to discuss the outcome. On one system you might get one result and on another system you get another result.

Also see https://stackoverflow.com/a/11589479/4386427

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63