Interest compounds annually for Stafford loans so all the interest that accrues after one year, will be added to the principal loan amount. As an example if you borrow $10,000 each year and don't pay the interest as it accumulates (assuming a 5 percent loan rate across the board which is very generous):
Year 1
Loan Amount $10,000
Interest $500
Year 2
Loan Amount $10,500 + 10,000
Interest $1,025
Year 3
Loan Amount $21,525 + 10,000
Interest $1,576.25
Year 4
Loan Amount $33,101.25 + 10,000
Interest $2155.063
Total Principal when you start residency: $45,256
If you pay the interest as it accrued (a total of $5000 over four years) your princpal savings will be over 10 percent. Your loan principal when you start residency would be the original $40,000 that you borrowed. With higher interest rates the savings would be much greater.