By putting code on SE (by posting it in a question or answer or another form of publicly posting it on the site) is the code given some sort of license automatically if one is not declared by the owner? So what I mean is for instance if person A wrote their own program and posted it as an answer to person B, if person A does not declare a license for the code, and then person B steals it and publishes it as if they were the owner giving no credit to person A, could person B get into any trouble if person A found out and wasn't happy?
All post content is available under CC-BY-SA.
But I disagree slightly with the spirit of muru's post. You can add additional licenses to your posts.
If a post declares its content to be available under an additional license, that is just dual-licensing. The reader can choose between the stated on and CC-BY-SA. This is just the same concept as if the writer of the post posted the content elsewhere (and declared a license).
However, you cannot override or subvert the CC-BY-SA license. It will always be an option for somebody reading a post here. If you post, you're making that content available under CC-BY-SA. Even if you add:
Ⓒ Oli 2015 All rights reserved, no copying my stuff!
... they can still copy it under the terms of CC-BY-SA.
This is why it is critical you own the content you post.
If you take code from somebody else that is GPL/MIT/etc —even if that declaration remains— you are adding another license with restrictive and therefore potentially incompatible clauses.
That may break the conditions of the license you had. Most licenses have an automatic termination clause meaning your post would have been made without license. It'd be copyright infringement.
If a copyright owner notices you using their content without license, they can sue you (in the US: for statutory damages, lost earnings and if they can prove you did it on purpose, a ×3 multiplier).
Ask Ubuntu is a US-based site but if somebody wants to find you, they could sue you in your country. If they can convince somebody that it's criminal infringement, they can attempt to extradite you (even for copyright) and if you have an estate or holdings abroad, they can sue that.
But fair use exists.
You can use limited amounts of copyright content and this bypasses the original license. What and how much you can use aren't black and white concepts though. It's also not the same around the world.
What holds up will essentially come down to common sense but again, if somebody wants to sue you for using their stuff, they're going to sue you.
All content posted to Stack Exchange, including code, is licensed CC-BY-SA. If you "declare" another license on your post, and that license is incompatible with CC-BY-SA, you're in violation of Stack Exchange's terms of service. CC is not a good license for code, but it is what it is.
... then person B steals it and publishes it as if they were the owner giving no credit to person A, could person B get into any trouble if person A found out and wasn't happy?
Emphasis mine. The license requires attribution. If B gave credit, there isn't any problem. Not giving credit is a violation of the license, and illegal (assuming you can prove it).