This is the correct way for getting the correct output.... However, childs parent id maybe sometimes printed as 1 because parent process gets terminated and the root process with pid = 1 controls this orphan process.
pid_t pid;
pid = fork();
if (pid == 0)
printf("This is the child process. My pid is %d and my parent's id
is %d.\n", getpid(), getppid());
else
printf("This is the parent process. My pid is %d and my parent's
id is %d.\n", getpid(), pid);