Hi ,
On Wed, Jun 18, 2008 at 4:44 PM, Wang Yu <wangyuict@xxxxxxxxx> wrote:
On Wed, Jun 18, 2008 at 6:54 PM, vaibhav khatavkar <khatavkarvaibhav86@xxxxxxxxx> wrote:
Hi Wang Yu ,
I saw ur code ...In ur first code snippet "%s" prints string located at p .. and thats null .. so we get output null ... (Note that : there are 2 printf statements)
char *p;
p = 0;
printf("%s", p);
printf("\n");
The out put is (null)
Now when u changed it like this ...
But, if I change into:
#include <stdio.h>
int main()
{
char *p;
p = 0;
printf("%s\n", p);
return 0;
}
The output will be Segment fault!
The segmentation fault comes just because in printf statement u have written "%s\n" .. compiler is unable to resolve it ... when u add a space between %s and \n the output is null and not segmentation fault ..
But why the complier is unable to resolve it when p is null? and it works when add a space after it?
when compiler comes across "%s\n" it considers %s\n whole as a token ...and is unable to to resolve ..
compiler only knows %s is string formating token and \n is new line token .. but don't know what %s\n token means ... and so gives segmentation fault ... :)
compiler only knows %s is string formating token and \n is new line token .. but don't know what %s\n token means ... and so gives segmentation fault ... :)
I don't know why....
Thanks!
--
National Research Center for Intelligent Computing Systems
Institute of Computing Technology, Chinese Academy of Sciences
--
with regards,
vaibhav
--
National Research Center for Intelligent Computing Systems
Institute of Computing Technology, Chinese Academy of Sciences
--
with regards,
vaibhav