What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 131 Stack Exchange communities.

Possible Duplicate:
How do I format my code blocks?

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); } }

share|improve this question

marked as duplicate by Michael Mrozek, random Oct 3 '11 at 0:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4  
Anyone editing that code block proper will nullify the question and its answers –  random Oct 2 '11 at 22:55

2 Answers 2

up vote 4 down vote accepted

See http://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.

share|improve this answer
    
Shouldn't that be language: lang-cs per this answer? –  hammar Oct 2 '11 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. –  agf Oct 2 '11 at 22:59
1  
@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. –  balpha Oct 3 '11 at 17:41

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); } }
share|improve this answer
    
I don't see anything about syntax highlighting, which is what he asked about? –  agf Oct 3 '11 at 0:28
1  
@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. –  Paulpro Oct 3 '11 at 0:34

Not the answer you're looking for? Browse other questions tagged .