Antacids like sodium hydrogen carbonates, magnesium or aluminium hydroxides neutralize the excess acid produced in stomach but their excess use causes production of excess of HCl in the stomach which is harmful and may results in ulcers.
Cimetidine and ranitidine work without such side effects as they prevent the interaction of histamines with receptors of stomach wall as histamines stimulates the secretion of the acid.