there is much easyer way
if str="123456"
sscanf(str,"%3d",&num);

num will be 123