Why people prefer gyroscope instead of compass? Does gyroscope replace compass? I would like to see the pros and cons of each.
First, to keep in mind: gyroscopes measure how fast you are turning and in what direction. Compasses measure your orientation to the earth's magnetic pole.
Positive Side of Gyroscopes:
With this in mind, we can see that gyroscopes won't be as sensitive to external magnetic fields as compasses will be. So, if there's an opportunity for your device to come close to a magnet, a compass may not be what you want.
Also, gyroscopes will work even in space, whereas compasses won't really work. (Side note: well, actually, the compass will still work, it just might not work in the way you want it to.)
Negative Side of Gyroscopes:
Since a gyroscope measures the how fast you're turning, and not really how far you've turned, you must perform numeric integration (of some form or another) to estimate your actual heading. This means that some error can be introduced. Along the same lines, because the sensor value for the current heading is dependent on all those that came before, any significant error in reading from the gyro could throw your calculations off permanently.
Also, a gyroscope is a relative reading, whereas a compass is absolute. The gyro will tell you how much you've turned since you begun the numeric integration, but it has no way of knowing if it started off pointing north or south.
The earth's magnetic field is rather doughnut shaped, so it isn't parallel to the earth's surface at most places. A compass is pretty good long as it is plumb but as the its rotational axis (or the electronic equivalent) changes, its reading does too. In a turning aircraft, f/ex, because the vehicle makes banked turns, the compass can lead or lag the actual heading.
It really depends on what you're trying to accomplish. Using the aircraft example again, the compass is used to find and hold a heading during straight and level flight. A directional gyroscope (one with a vertical axis) is used to read the heading during turns. The directional gyro gradually drifts so it needs to be set to the compass periodically.