Possible Duplicates:
How to post code in SO
How do I format my code blocks?
When I post code here it ends up all mangled. How do I make it look pretty?
For example, posting //#include (minus the //) ends up looking like this:
include
What am I doing wrong?
-
1Isn't there an explanation to the right of where you type the question?Earlz– Earlz05/25/2010 18:46:08Commented May 25, 2010 at 18:46
-
Actually there doesn't seem to be a duplicate for this question, though an old one of mine is related meta.stackexchange.com/questions/34588/… Maybe this should be a FAQ?Earlz– Earlz05/25/2010 18:47:19Commented May 25, 2010 at 18:47
6 Answers 6
If you post code or XML, highlight those lines in the text editor and click on the "code" button (101 010) on the editor toolbar to nicely format and syntax highlight it.
-
7@marc_s -- not a circle, not freehand, it is red and points in roughly the correct area; I give it a 5.5.tvanfosson– tvanfosson05/25/2010 20:26:14Commented May 25, 2010 at 20:26
-
4@tvanfosson: out of 6? Not bad..... heck - I'm a dev, not a graphics artist! ;-) You don't want to see my complete UI's......marc_s– marc_s05/25/2010 20:29:13Commented May 25, 2010 at 20:29
-
This method worked for me, It will automatically detect the language. Example : Java for me.vanduc1102– vanduc110205/03/2015 11:21:37Commented May 3, 2015 at 11:21
-
3Only works in answers, not in comments on answers. However, backticks work in both.Dave Burton– Dave Burton02/26/2016 12:17:51Commented Feb 26, 2016 at 12:17
-
I don't see the 101010 button when writing a question.quant_dev– quant_dev03/20/2016 09:21:24Commented Mar 20, 2016 at 9:21
-
Since some time, you can run code snippetuser3123159– user312315912/06/2020 12:16:20Commented Dec 6, 2020 at 12:16
You surround your code in backticks ``
(like this
)
Or for block code, you indent your code by four spaces:
function test()
{
alert("it works!")
}
You can view the source for this post here.
-
The post's link seems to be broken.Hassan Ahmed– Hassan Ahmed05/31/2019 21:43:42Commented May 31, 2019 at 21:43
You can also use keyboard shortcut: just highlight and hit ctrl-k
-
The shortcut is such a life-saver for me. Pressing it while writing a comment initiates for me Google's Search in the address bar in Chrome, which was also a nifty little shortcut for me to find out about.Hassan Ahmed– Hassan Ahmed05/31/2019 21:48:24Commented May 31, 2019 at 21:48
If you want to do this manually, you add 4 spaces in front of every line of code.
With the editor, there is a code button (101010) you can press after highlighting the code.
like this, highligh what you want then click the code button
#include
-
1you can use this shortcut ctrl+kAbdul K Shaikh– Abdul K Shaikh01/30/2015 20:43:52Commented Jan 30, 2015 at 20:43
I think this is one of those things that should belong on meta.stackoverflow.com or some such, but... in the post box, if you click the orange question mark in the top-left of the box, it will take you to a page with Markdown help. Basically, you prefix each line with four spaces:
like this
and you should see the text show up as 'code' in the preview area below the edit box.