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

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

Making Empty C++ Project: General exception (Exception from HRESULT:0x80131500) Visual Studio Community 2015 -

How to fix java warning for "The value of the local variable is not used " -