This is based on the real world to a great extent.
For a lot of the history of firearms the individual weapons used by infantry soldiers have been quite long and somewhat unwieldy, requiring both hands to handle this is fine for ordinary soldiers as using their weapon is their main task in combat. However officers and senior NCOs might also have to use radios, binoculars, maps etc so having a long rifle was a major encumbrance. Equally they would not necessarily be expected to use their weapon much apart form for personal protection as their main job is to coordinate their own troops, communicate with their own superiors and maintain awareness of what is going on in the battlefield as a whole. So a smaller weapon like a pistol or sub machine-gun was more convenient and more useful in the sort of close quarter fighting where they would need their personal weapon.
There is also the fact that more senior and staff officers tend to have more of an administrative type role where a large weapon would be unnecessary and very impractical.
Towards the second half of the 20th century this trend started to reverse as infantry rifles became more compact, lighter and less of an encumbrance. There is also the consideration that having a recognisably different weapon makes leaders easier to identify by snipers.