I have the following program : My program compiles fine and gives the output as mentioned below . I have some question on output which is listed at the bottom. *******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char *p = malloc(sizeof(char));
char *q = malloc(sizeof(char));
printf("address of p = %p \n", p); A
printf("address of q = %p \n", q); B
strcpy(p, "abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz");
printf("Value in P : %s \n", p); C
printf("Value in q : %s\n", q); D
printf("string length of P : %d \n", strlen(p)); E
printf("string lenght of q : %d\n", strlen(q)); F
return 0;
}
===OUTPUT ==
address of p = 0xbbf010
address of q = 0xbbf030
Value in P : abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz
Value in q : 789abcdefghijklmnopqrstuvwxyz
string length of P : 61
string lenght of q : 29
=====OUTPUT END==
Questions :
1. Why there is a difference of 32 bytes between address of p and q. I have allocated 1 byte only for P. How automatically 32 byte difference between successive malloc
?
2. I have not NULL
terminated my Strings. How printf
detecting the \0
termination?
3. How strlen
is also working fine without a \0
termination?