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

Popular posts from this blog

Google sheets equipment borrowing system -

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

c# - Convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.IList<>' -