You should ideally be getting a segmentation violation runtime error.
You have assigned a pointer to a string literal which resides in read-only memory. Trying to modify that is bad!
Try copying it onto the stack
char a[] ="hello,world)";
If you really have to use dynamic memory (please write that in your question) then you have to manually copy your string there:
char *a = malloc(sizeof("hello,world)"));
memcpy(a, "hello,world)", sizeof("hello,world)"));
int a_len=strlen(a);
a[a_len - 1] = '\0';
Alternatively you can also have printf truncate your string:
printf("%.*s", strlen(a) - 1, a);
Also as Basile pointed out there is strdup
char * a = strndup("hello,world)", sizeof("hello,world)") -2);
Note that here we have to truncate by two characters because sizeof
includes the null terminator, but strndup
will always add one.