How to display C code in a wordpress.com blog post

This post shows how to insert C code into a wordpress.com blog post. When everything is working correctly, the C code should appear like this to someone reading the blog post:

//
// Flashing LED example for PIC18F4620
// Written by Ted Burke
// Last updated 10-4-2013
//

#pragma config OSC=INTIO67,MCLRE=OFF,WDT=OFF,LVP=OFF,BOREN=OFF

#include <xc.h>

int main()
{
	TRISD = 0b11111110;

	while(1)
	{
		LATDbits.LATD0 = 1 - LATDbits.LATD0;
		_delay(125000);
	}
}

Here’s how the C code above looked when I inserted it into the post in the WordPress editor (click to enlarge):

sourcecode_screenshot

Opening and closing “sourcecode” shortcodes have been included just before and just after the C code. When adding the opening “sourcecode” shortcode, you have the option of specifying the programming language, which will enable colour-coded syntax highlighting. Here, I’ve set the language to “cpp” which means C or C++.

Important: Please note that I switched to “Text” view (rather than “Visual” view) before inserting the code. I recommend not switching back to Visual view at all once you have inserted code in your post, because switching back and forth seems to cause parts of the code to become corrupted, especially the symbols “<” and “>” which appear frequently in C code (for example in include directives and boolean conditions).

References

  1. Posting Source Code – Support – WordPress.com
Advertisements
This entry was posted in Uncategorized. 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