Yesterday I was flying in my simulator with the Cessna 172 when I thought about simulating a fault in the plane's electrical system (alternator and master). But then I was in doubt, if I can't contact the tower or enter a code on the transponder, how can I inform that I need to land on a runway?
In the U.S. (other jurisdictions may be similar)...
Avionics are a convenience in VFR day flight. You should be able to fly your airplane by sight picture alone. There are aircraft flying today without any electrical system at all. In a fixed-gear aircraft, none of the equipment required by Part 91.205(b) require electric power. There are standard protocols in place if you have to fly to a destination without electric power.
If you are not on a flight plan or flight following when you lose electric power, stay clear of Classes A, B, and C airspace as well as the 30 nautical mile mode C veil around Class B airports, and the airspace above Class A, B, and C. Stay or descend below 10,000 feet MSL if possible or 2500 feet AGL when not possible to descend below 10,000 feet MSL.
If you lost all electric power before communicating your situation and intentions to ATC, you can assume that squawking 7600 on your electric transponder will do no good, either. Find a VFR Class D, E, or G airfield where you can land. Land as soon as practicable. Remember, you can not enter Class D airspace (normally a 4 mile radius from the field) until you have established two-way communications with them. You can, however, circle above the traffic pattern or tower at an altitude outside of the usually 2500 foot AGL ceiling of Class D until you get light gun signals from the tower. Wagging your wings or following their instructions will be your acknowledgement of light gun signals. Two-way communication will then be established.
Remember, an active IFR plan will give you clearance to your declared destination or declared alternate(s) at your ETA. Neither a VFR flight plan nor flight following prior to an expressed clearance will give you clearance into A, B, C or D airspace.
You should follow this plan of action at any Class D, E, or G airport with an operating control tower. Typically (but not always), a Class E or G airfield will not have an operating control tower. At those airfields, follow the AIM recommendations for entering the traffic pattern for a landing. An overhead flyover well above traffic pattern altitude would be advisable beforehand. Be very cautious of existing traffic. And, make your flight path as predictable to other pilots as possible. Although communication is strongly recommended, it is not required.
If you are flying on an IFR flight plan in IMC, follow the directions of Part 91.185.
If you are flying VFR at night, make it a habit to have the phone numbers of nearby towers, TRACON, and ARTCC in your cell phone. Call them to tell them of your predicament. Declare an emergency via cell phone. Then, use your Part 91.3 PIC authority to request clearance to land at any airport where the runway lights will be lit regardless of airspace or class. ATC can coordinate your clearances, then call an airport official or radio a passing airborne aircraft to turn the light on for you. You won’t be able to do it yourself without an operating radio. And, landing at a dark airport without a landing light will be challenging at best.
If you have a total electrical failure, then you should try to find a suitable non-towered field to land at. In a C172, you have about 15,000 choices in the US, and unless you're in the mountains, probably only a few minutes away from a decent one. The number of choices goes down as aircraft size goes up, but there shouldn't be any trouble finding one as long as your engine is still running. Since a radio isn't required at non-towered airports anyway, just enter the pattern and land as usual.
If the only suitable airport is a towered one, then head directly there well above pattern altitude, circle the field, watch for light gun signals from the tower, and then land when they tell you it's safe.
Next time you're at a (real) towered airport on a slow day, ask them to demonstrate the light gun so you know what it looks like. I don't know if sims have that feature, though.