As someone who has also been in your position I can tell you that if you play in an official (nationally) rated competition in country B then you need to join their chess federation and pay the appropriate level of membership. This helps pay for running chess in that country. Sometimes the charge is disguised by you paying a higher entry fee for the tournament than somebody who is a member.
If you play in a FIDE rated tournament then you have to be a FIDE registered player with a FIN. The federation is responsible for making sure that unregistered players do not play and the federation is fined by FIDE if they break this rule. Like the national chess federation, FIDE costs money to run.
However the national federation is also responsible for registering players in that country. This can be done after the tournament is played as long as it is done before the tournament results are submitted to FIDE and most federations will do this provided they have your details, full name and date of birth.
For nationals of that country this is normally an automatic process. However if you are a national of another country you may prefer not to have FIDE registration with a different country and your permission is usually required by the federation before they "adopt" you.
Note also that while some countries are a bit backward regarding FIDE rating - most of their competitions are nationally rated only - in some countries all adult and most junior tournaments beyond weak players are also FIDE rated. In these "full FIDE" countries the only reason that tournaments for weak juniors are not FIDE rated is because they want to relax rules around number of illegal moves and use of clocks.