Posting C code on wordpress.com

I see a lot of people (including many of my students) having problems posting C code on wordpress.com blogs, even when they use the “sourcecode” shortcode which is provided for exactly this purpose (see here for details). The following are some of the commonest symptoms:

  • Lines of code containing the characters ‘<‘ or ‘>’ (which includes a lot of “#include” directives or lines containing boolean conditions) get corrupted, presumably because they are interpreted as HTML tags or something like that.
  • Code indentation gets modified or obliterated, which makes the code very hard to read.

It’s frustrating when you post code, then notice later on that it is appearing incorrectly. Fortunately, there is a sequence of steps for posting C code that works every time and eliminates the problems mentioned above. These are the steps:

  • Create a new post (or edit an existing one).
  • Make sure you are editing the post in the “Visual” tab.
  • Write the text that will appear in your post just before and just after the code listing.
  • Leave a blank line where the code listing will appear.
  • Switch to editing the post in the “Text” tab. You are now viewing the same post with more of the underlying formatting settings visible in plain text form.
  • The blank line that you left where the code listing will appear should now appear as “&nbsp;”.
  • Replace “&nbsp;” with the following two lines:


      This is the paragraph before the code.

      [sourcecode language="cpp"]

      [/sourcecode]

      This is the paragraph after the code.

  • Open your C code file in another editor (e.g. Notepad++) and make sure it’s all neatly indented.
  • Copy and paste the C code into your post, placing in between the opening and closing “sourcecode” shortcodes, as shown in the following example:


      This is the paragraph before the code.

      [sourcecode language="cpp"]
      // example C code

      #include <stdio.h>

      int main()
      {
          printf("Hello\n");
          return 0;
      }
      [/sourcecode]

      This is the paragraph after the code.

  • This step is critical: Switch back to the “Visual” tab.
  • Now click “Publish” (or “Update” if you’re editing an existing post).
  • View the post to confirm that the C code is appearing correctly.

To conclude, here’s a complete worked example in screenshots.

Begin by creating a new post and adding some text in “Visual” view:

Switch to “Text” view and add the “sourcecode” shortcodes where you want your code to appear:

Paste your C code in between the “sourcecode” shortcodes:

Switch back to “Visual” view:

Publish and view your post and check that the code appears correctly:

Advertisements
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s