We present a theoretical analysis of tip-sample interactions in scanning-tunneling and atomic-force microscopy with atomic resolution, based on ab initio calculations of electronic structure, total energy, and forces. Our results for model systems consisting of a graphite monolayer and of 2×2 or 3×3 arrays of aluminum-tip atoms at high-symmetry sites indicate that at separations below 4 AI the tip already induces changes in electronic structure accompanied by significant charge rearrangements. In particular, site-specific localized states appear and provide a net binding interaction. Because the tip admixes states different than those near the Fermi level of pristine graphite, the tunneling current can deviate considerably from the commonly assumed proportionality to the local density of states of the unperturbed sample. Drastic changes occur at shorter distances where an overall repulsion prevails and where most measurements have been made to date. The ion-ion repulsion is found to determine the force corrugation in that range and up to the separation corresponding to maximum attraction. For the system considered here, the net attractive force on the tip and the corresponding gradient are weaker at the top site than at the hollow site. © 1990 The American Physical Society.