In general, yes, if practical.
The goal for most questions on SO is to include a Minimal, Reproducible Example (MRE). This has many advantages for askers and answerers:
- The code compiles. Unless of course the question is about a compilation error. This is useful because anyone interested can download and run the code themselves, which is useful because ...
- The code can be verified against the asker's assumptions. Sometimes askers who don't include an MRE will assume that the problem in their code lies in one place, and post just that snippet as a question, when really the problem is in a completely separate area. Potential answerers can download the MRE, run it, and close the question as being inaccurate if the code is misrepresented.
- External factors can be reduced or removed. Having random people on the internet able to download and run your code is a great way to find out that your problem only exists when your particular compiler, operating system, etc. is used. This can be difficult to find out without the help of a wider community, and the harder you make it to run your example, the less likely you are to get this kind of help. In fact, requiring anything more than just copying and pasting is going to make it very unlikely that anyone runs your code.
- Creating an MRE is a form of debugging. Having to pare the question down to its essentials often reveals where the problem is without even requiring a post on SO. Of course this doesn't apply when editing an example to compile, but it can help get users in the proper habit.
- It makes it clear that the user put in effort. For better or for worse, there is a culture of downvoting and closing questions in which the asker didn't appear to try very hard. Going through the effort to create an MRE is a great way to show what you've tried (bonus points if you use the phrase MRE!).
Of course, this isn't practical for all questions. Questions involving the use of multiple libraries or frameworks will often require so much boilerplate for an MRE that it would make the question utterly unreadable. This can also be the case with embedded programming, where nobody has the device you're using anyway, and probably in many other situations.
For your particular situation, was it useful?
I would argue that yes, it's a good idea for your question. It allows people to easily download and run your code. If somebody has encountered your exact situation before, they won't need to do this; but for those of us who haven't, answering your question may require some investigation. I might need to run your code, check the output, form a hypothesis about what's happening, verify my hypothesis ...
Remember, if your question requires some digging in on my part, you're now in a contest between the effort I have to put in, and the interest I have in an answer. Making your question more interesting is hard - you have a fundamental problem you need an answer to, and that's not easy to dress up. In contrast, reducing the effort I have to invest is much easier and worth doing. If you have a question about a particular library function, link to its documentation; if your concern is about some language standard, tell me where I can find the standard text; and if you have a problem with a program, make my life easier and let me quickly run and iterate on that program.
Of course, this is all just my opinion and not an official policy as far as I know, and it might be that the community at large disagrees with this view. If so I suspect we'll quickly find out from the vote counts.