What is the use of RC filter in front of Analog port pin while using port pin for analog to digital conversion in pic 16f877A and in most pic controller the RC filter is advised to be used?
The idea is to filter out noise and bandlimit the signal to avoid aliasing. You therefore want to set the corner frequency of the filter above the maximum signal frequency, but make sure you have no significant signal or noise above half of the sampling rate. With a single-pole filter you will have 3dB of attenuation at the corner frequency and 20dB per decade of frequency rolloff above that. The corner frequency for an RC filter is f = 1/(2*pi*R*C), so choose your components so as not to interfere with your signal. You may need more than a single pole filter to avoid aliasing depending on your signal and noise frequency content.
Of course if you're just taking a sample of some slowly varying signal like a temp sensor or something like that a single pole filter will be fine.
A lot of MCUs take a pulse of current to "charge" the internal sample and hold circuitry (part of the integrated ADC). If the series resistance of the signal source is too high, it won't charge the internal cap sufficiently and there will be an error in the conversion.
Applying a capacitor from the pin to ground means that there is a much greater chance of this working because the external cap can supply the current needed. See this SE post (relating to PIC input impedance) and check out the answer.
Of course this makes a low pass filter that is also useful for anti-aliasing.
Impedance: as said by @Andy aka, you ADC has a small capacitor inside to provide the sample and hold functionality of the ADC. You have to quickly provides the charges to that capacitor. You may use a low impedance buffer in front of the ADC. Or use a capacitor that is several times bigger than the one into the ADC.
Anti-aliasing: You will often see that you have to ensure that the frequency content of the signal at the input of your ADC is at least half the sampling rate (fin < fs/2). But why? Because of the signal processing theory. The Nyquist–Shannon sampling theorem said that if you want to be able to sample your signal without any information loss, then the sampling frequency should at least twice that frequency.
Ok, fine. But do I need this?
Good question! Two answers:
No: If you use the signal that enters the ADC to compare against a threshold (for instance when a tank reaches a certain level). Fine. You got a sample that said that the tank was at that level, this is fine. The sample reflects the reality. At least if the noise level on your signal is low.
Yes: If you plan to use any signal processing computations. Then your samples are not just numbers, they represents a signal. And this representation matches the reality only if the Nyquist–Shannon sampling theorem is verified.
Then it's up to you. You want to know the voltage at certain time: the ADC will tell you this with or without an anti-aliasing filter. You want to consider your input as a signal and perform things such as filtering: You need the anti-aliasing filter.