A uncharged capacitor C is connected to a battery with potential V
Since the voltage across a capacitor must be continuous (for finite current), one cannot specify this and expect to get valid answers.
Remember, for the ideal capacitor
$$i_C = C \dfrac{dv_C}{dt}$$
But specifying that an "uncharged capacitor C is connected to a battery with potential V" is equivalent to specifying
$$v_C(t) = Vu(t)$$
But this is equivalent to specifying
$$i_C(t) = CV\delta(t)$$
which certainly ejects this problem outside of the realm of ideal circuit theory.
Now, there is an easy remedy for this - place a resistor in series with the capacitor and then the capacitor voltage becomes
$$v_C(t) = V(1 - e^{-\frac{t}{RC}})u(t)$$
and the current is finite
$$i_C(t) = \frac{V}{R}e^{-\frac{t}{RC}}u(t)$$
Now, it is straightforward to show that the energy stored in the fully charged capacitor is less than the energy supplied by the battery with the difference being the energy dissipated by the series resistor.
Finally, $R$ cannot be reduced to zero - one can show that there is a fundamental radiation resistance associated with the circuit such that energy from the battery can be radiated away during charging.
My question is: What is the work done by the the battery?
When the polarity is reversed, the capacitor will initially discharge, doing work on the battery, until fully discharged and then the battery will again begin doing work on the capacitor.
Since there is loss during the charging / discharging process, one cannot equate the work done by the battery to the work done on the capacitor.
Perhaps the problem is to be solved assuming that the capacitor
connected to a battery gets approximately a charge CV(in negligible
time).
Including a series resistor allows one to compute the energy dissipated by the resistor which turns out to be independent of the resistance and equal to
$$W_R = \frac{CV^2}{2}$$
Even an ideal capacitor cannot be losslessly charged to a potential E from a potential E without using a voltage "converter" which accepts energy at Vin and delivers it to the capacitor at Vcap_current.
If you connect an ideal voltage source via a lossless switch to an ideal capacitor which is charged to a lower voltage, infinite current will flow when the switch is closed. If you introduce a very small resistance in the charging path the current will be large and large losses will occur despite the resistor's small resistance. As you increase the resistor the current will decrease but the increasing R causes increased power loss.
The closest real world approximation to lossless capacitor charging is to use a buck converter.
Best Answer
Short answer: this is a textbook example of the limitations of ideal circuit theory. There seems to be a paradox until the underlying premises are examined closely.
The fact is that, if we assume ideal capacitors and ideal superconductors, i.e., ideal short circuits, there appears to be unexplained missing energy.
What's not being considered is the energy lost to radiation at the moment the two capacitors are connected together.
At the moment the capacitors are connected, in accord with ideal circuit theory, there should be an impulse (infinitely large, infinitely brief) of current that instantaneously changes the voltage on both capacitors.
But this ignores the self-inductance of the circuit and the associated electromagnetic effects. The missing energy is transferred to the electromagnetic field.
From the comments:
and
If you find yourself in agreement with the comments above, consider the following excerpt from the exercise "A Capacitor Paradox" by Kirk T. McDonald, Joseph Henry Laboratories, Princeton University:
And then, in problem 2: