30

How do I get the following to show up (on stackoverflow) with color syntax highlighting?

class Foo { internal Foo() { for (int i = 0; i < 42; ++i); } }

2
  • 6
    Anyone editing that code block proper will nullify the question and its answers Commented Oct 2, 2011 at 22:55
  • For the list of all supported languages: meta.stackexchange.com/questions/184108/… Commented Dec 9, 2023 at 1:29

2 Answers 2

52

See https://stackoverflow.com/editing-help.

Basically, for code, indent four spaces -- you can do this by pasting code then highlighting and hitting Ctrl + K or the {} button above the editing box.

As far as syntax highlighting, see Changes to syntax highlighting and Interface options for specifying language prettify.

The correct language will often be inferred by the tags on the question, but you can manually specify it with an HTML comment:

<!-- language: c# -->

    public static bool IsAwesome { get { return true; } }

or

<!-- language: lang-js -->

    setTimeout(function () { alert("JavaScript"); }, 1000);

before the code block.

11
  • Shouldn't that be language: lang-cs per this answer? Commented Oct 2, 2011 at 22:56
  • That is a quote straight out of the official editing help page. If it's wrong, that definitely needs to be corrected. There are four examples of syntax highlighting comments, and other three do use that format. I'll add the JS example. Commented Oct 2, 2011 at 22:59
  • 3
    @hammar: Both work; you can either use the prettify language code (lang-cs) or the tag (c#), because lang-cs is the default syntax highlighting language for the c# tag. Commented Oct 3, 2011 at 17:41
  • The HTML language comment thing doesn't work. Not in this example, not in my posts. Commented Jul 20, 2016 at 12:35
  • 1
    @aalaap Did you do it exactly as shown in stackoverflow.com/editing-help#syntax-highlighting ? Is the post on Stack Overflow or another site? They may not be exactly the same. Commented Jul 20, 2016 at 19:12
  • @agf It's right here on StackOverflow. I tried JS, C# and PHP, but all I can see is the <!-- language: xxx --> comment as a part of the grey, monospaced code block. That's what I see right up here in this answer too. Commented Jul 21, 2016 at 8:36
  • I have found that ```php also works. Commented Dec 23, 2019 at 17:46
  • @Neuron please don't make useless edits to an ancient post. Commented Aug 11, 2022 at 14:14
  • @Luuklag I don't think making a post more readable is useless. Also, what does editing have to do with the age of a post, if it is still very relevant for readers today? Commented Aug 11, 2022 at 14:28
  • @Neuron it's not relevant, as it was already closed as a duplicate 10 years ago. Commented Aug 11, 2022 at 14:29
  • @Luuklag It being a duplicate is not an argument as to weather it is relevant to some people. By that logic, we could delete duplicates instead of closing them. I got here by googeling code highlight stackexchange, and this is the first result. Commented Aug 11, 2022 at 14:37
0

If you want to insert code inline like this int i = 0; use backticks: `int i = 0;`. If you want an entire block of code you can highlight it and then click the code button which looks like {}. You can also hit Ctrl+K while it's highlight, or just manually indent it all 4 spaces:

class Foo { internal Foo() { for (int i = 0; i < 42; ++i); } }
2
  • I don't see anything about syntax highlighting, which is what he asked about? Commented Oct 3, 2011 at 0:28
  • 4
    @agf That's because this is Meta not SO. Meta isn't for posting code, so it has no syntax highlighting. This exact post on SO will have syntax highlighting. Commented Oct 3, 2011 at 0:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.