How to Track domain and subdomain using same Google analytics code

Hello Guys,
I want to track main domain and sub domain of my website uswing same google analytics code. Can any one help me. I have put the same code on both the website. But, It treats same user as different user when user switches website from main domain to subdomain. Is there any way to do this. Please help me