When the problem is that you have code too big to include in the answer, assuming you're willing to make the code public, the best solution is usually to host it elsewhere and link to it. You can use a pastebin for this (such as pastebin.com, or, as we often use on Ask Ubuntu, paste.ubuntu.com — there are many others, of course).
If one of the reasons the source code should not be included in a post on a Stack Exchange site pertains to its licensing, and the code is in a pastebin, I recommend raising that point explicitly in your answer. (This may help to prevent others from editing some of it into your question, if their edits would be beyond the limits of fair use.) If the code is somewhere like Github, then presumably it's part of a source tree with a copyright statement...
In any case, it's advisable for every source code "file," whether it's a real file or simply a page in a pastebin that may or may not correspond to an actual file, to contain a prominent, visible copyright statement at the top, with licensing information if applicable. That advice doesn't really have anything to do with Stack Exchange--it's just good practice, generally. It's good practice even for source code that is not intended to be released to anyone at all. (Please note that you do not need a copyright notice for your source code to be copyrighted, however. Under U.S. law since the 1970's and international treaties, a work is copyrighted by its author automatically upon creation, and the copyright must be explicitly disclaimed in order not to exist. Oh, and I'm not a lawyer and nothing in this post is legal advice.)
(If the source code is public domain, then of course it should not have a copyright statement, but you can still have a statement indicating that it is public domain.)