First off, let's be clear what it is we are talking about. There is a world of difference between shooting an unsuspecting bear and one that has you in his sights and is stalking or charging you. In a hunting situation, even large bears are not difficult to kill with a well placed shot, and minimum calibers are often used successfully. The situation changes when the range is measured in feet rather than yards. Fear is not conducive to good shooting, and a shot which would knock down an unsuspecting bear will scarcely be felt by one loaded up on adrenaline.
Another fact to consider is that bears, at least the ones around here, do no respond well to live fire, and are as likely to approach you as they are to run away. A cracker shell gun is the correct tool for chasing a bear away. When that fails to work, you can step up to a lethal deterrent. Don't wast your ammo firing warning shots or trying to kick dirt in his face. If he has moved within your comfort zone kill him.
What is the best firearm for this purpose? Well, as stated by previous posters, if we conclude that a .223 carbine is not the answer, a 12 gauge pump or auto shotgun is a good place to start. Pick a model with a manageable barrel length, no longer than 24", and consider a model with rifle sights, which are useful when shooting slugs. I am not really on board with those who suggest getting the cheapest gun available. If that is all your budget allows, then you will have to make out as best you can, but the guns priced in the moderate to expensive range tend to be more reliable, fit better, and are more user friendly.
Purchasing the gun is not enough. now you need to practice so that you are competent if faced with an emergency. You will probably have to fire 400 rounds of ammunition before you reach a competent level. Do not attempt to do this in too short a period of time. First you must shoot enough to familiarize yourself with your new gun. Then you can begin working on marksmanship, and finally on timed drills and moving targets. Once you are competent, you will have developed the confidence to handle most situations.
Another important consideration is learning about bear behavior. There is a wealth of information out there, but I would direct you to the books by Gary Shelton and by Steven Herrero. Also sign up for any bear defence classes which may be available in your area.