Unexpected Output
Title
Question
#include<stdio.h>
#include<string.h>
int main()
{
<span style="white-space: pre;">\t</span>
char strname[30]="Spoken-Tutorial";
<span style="white-space: pre;">\t</span>char s[]={'a','b','c'};
printf("The string is %s\\n", strname);
<span style="white-space: pre;">\t</span>printf("String: %s\\n",s);
return 0;
}
For the above code output is following:
The string is Spoken-Tutorial
String: abcSpoken-Tutorial
Why string s is containing such value?
C-and-Cpp Strings 02-03 min 10-20 sec
Answers:
In C you need to terminate a string with a 'null' character. The above program can also produce a segmentation fault on some machines, as the string s is not null terminated.
Login to add comment