Stack Overflow is a well-known source for programmer questions, with many insightful and helpful contributions. In particular, many people contribute by posting snippets, short pieces of source code that solve a particular problem or address questions of others. As snippets are (usually) protected by copyright, this raises the question which license would apply.
Copyright exists automatically whenever someone creates a “work of authorship”. This includes software. The only requirement is that the work is the author’s intellectual creation, i.e. there was some creativity involved in the work.
For software this is almost always the case. A programmer has to make many choices when writing software, from choosing the program flow to dividing up the functionality into functions and modules, and such choices provide the required creativity.
For snippets this may be different. A snippet can be a very short and simple solution. For example, a snippet can show how to call a particular API. That would not be creative enough, as it is ‘merely’ explaining or demonstrating a certain solution.
As a rule of thumb, if two programmers would provide substantially the same piece of code, the code is not creative under copyright law. This may happen if the code is very short, but longer code for a highly constrained environment (e.g. embedded software that needs to call a number of functions and perform well-defined given actions in between) could also be uncopyrighted for this reason.
However, a snippet that is more than one or two lines of standard function calls would typically be creative enough for copyright. Already at that level a programmer has to make creative choices. There is an often-quoted rule of thumb stating that anything less than ten lines of code is “trivial” and therefore not copyrighted, but that rule of thumb is not supported by any copyright law I’m aware of.
If a snippet is copyrighted, it can only be used with a license. This is a problem with Stack Overflow code, as few contributors explicitly add copyright or license statements to their contributions.
The Stack Overflow TOS is the next place to look. Here it gets a bit confusing, as the TOS notes that user contributions (including source code) are licensed Creative Commons Attribution-ShareAlike (CC-BY-SA) but then goes on to explain what this means as follows:
These four requirements however do not mention the ShareAlike requirement. In brief, ShareAlike means that changes to such Subscriber Content have to be made available to downstream recipients in source form (similar to the GPL requirements in open source).
Stack Overflow founder Jeff Atwood in 2009 explained:
The cc-wiki license seems pretty clear to me on this point: free to remix and reuse, as long as you attribute and use a similar license.
That does confirm that the ShareAlike requirement is in full effect. Atwood however goes on to say:
That said, a snippet of code falls under excerpt category and thus should be free to use under fair use.
This apparently suggests that he sees reuse of snippets as fair use, or under European copyright law as a quotation. That seems to be incorrect: copying a piece of code in your own software is not the same as copying some text in your own article. Quoting or using excerpts requires some context of your own, a functional reason, such as replying to it, supporting your own argument with it or contrasting it with another source. No such context is present when reusing software. So, I don’t see a fair use or quotation argument for source code snippets.
So, in summary: code snippets on Stack Overflow are protected by copyright unless they are so small that any two programmers would come up with substantially the same code. If they are copyright-protected, the StackOverflow license applies unless the poster has indicated otherwise in his post (or has separately granted a different license). The StackOverflow license requires attribution and sharing of changes to downstream recipients of the code.