c - Passing NULL to printf %s -
this question has answer here:
#include<stdio.h> #include<string.h> int main() { char *ptr = null; printf("%s", ptr);//the output null // printf("%s\n", ptr); //addition of **\n** give segmentation fault return 0; }
the first printf
outputs: (null)
. why second printf
's output is: segmentation fault (core dumped)
on adding: \n
?
printf("%s", ptr);
here printf
expects valid pointer, points null terminated string, can't pass null it. if so, trigger undefined behaviour, , can't reason output of program.
ps. have found answer might have more details thing might interested in. see here. question seems duplicate of one.
Comments
Post a Comment