1

I'm aware that Stack Exchange uses the Google prettify to add syntax highlight to code we use in our answers, but how do you specify in what language your code is, if you want to specify it explicitly than let the Google prettify decide?

Till now I have been using the <pre> tags to mark where the code begins and the </pre> to mark where it stops. After searching I found that you can specify the language by using <!-- language: C --> for example and your code below. But how do you mark where the code ends? Or do you put it at the top and it works for all the code blocks in <pre> that follow? So it should be like:

<!-- language: C -->
<pre>
#include ...
int main(void)
{
    ...
}
</pre>

Thanks for any help!

migrated from cs50.stackexchange.com Jul 8 '15 at 15:47

This question came from our site for students of Harvard University's CS50.

2

by default, the language is detected automatically. but in case you wanna specify a language (e.g., if the code is not highlighted correctly), you may use

<!-- language: lang-or-tag-here -->

for example

<!-- language: lang-c -->

if you don't want the snippet to be highlighted, you may use

<!-- language: lang-none -->

in this answer is a list of all the language codes.

also you don't have to use the <pre> tags to format code. you just highlight it and press ctrl + k or just click on this icon enter image description here.

the language code that specifies the language of syntax highlighting and the actual code should be in that format

<!-- langauge: lang-c -->

    // code goes here

for example

<!-- language: lang-c -->

    int main(void)
    {
        // code
    }

is rendered as follows:

int main(void)
{
    // code
}
  • 1
    Note that CTRL + K or the {} icon merely cause the highlighted code (or the cursor) to be indented 4 spaces, and it's that indentation that causes Markdown to parse the text as code. So a) you don't strictly need to use the button or shortcut and b) you can wind up with code formatting even when you didn't use the button or shortcut (possibly undesired). – Air Nov 6 '15 at 17:07

You must log in to answer this question.

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