In the EU, you own what you wrote (papers or programs) except if it's contract work, which precludes a contract that either states clearly what you are to make, or a general employment as programmer or artist or such, and of course... being paid.
Some companies (mostly the three major US consulting companies) are trying to outmanueuver the law by making people sign work contracts that basically say they own anything you produced while employed, including what you wrote in your free time. This is of course entirely illegal, but most people accept it out of fear of losing their job and being ostracised.
SimonForsman's stance on public universities is ethically correct. Public universities (all materials and employees, including e.g. professors and research assistants) in the EU are paid exclusively by tax funds. Insofar it is only just and reasonable that what they produce belongs to the public. Of course, students aren't being paid, so that's a different thing.
In practice, it happens that university employees patent/sell their work anyway, and there exist non-profit associations which are freed from paying tax and governmentally subsided, which develop technologies using governmental funds, patent them, and enforce the patents, worldwide. Did I hear anyone cough MP3?
Also, in practice, if you write something that's outstanding, all of it belongs to your professor (even if he didn't do a thing). And you had better shut up about it, if you're interested in ever having an academic career.