Jump to content

Welcome, Guest!


Tutorial: Getting Started with C

Recommended Posts

Getting Started with C

This is a quick and very simple introduction to programming in C on the Raspberry Pi. The program is the sort of program I use to write on the BBC Micro in the '80s in the UK electrical retailer Laskys to annoy the staff!

On Windows I used win32diskimager to write the debian6-19-04-2012.img file to a Kingston class 4 8GB SD card. a 2 or 4GB card would do just fine. On Windows 7 you will need to run the win32diskimager as administrator to access the SD card. The debian image and the image writer are available in the downloads area on the raspberrypi.org site.

Ok, so having written the debian image to the card, make sure the card is pushed all the way into the Pi & ensure you have a keyboard, mouse and monitor connected. My setup has the Pi linked to a 800x600 touch screen via a HDMI to DVI cable - I hope to get the touch screen working at a later date.

When the Pi has booted - if it's the first time then the system will perform some extra first-boot initialisation before restarting. Nothing very much happens after booting - you will be left looking at a black screen with loads of text and a login prompt. Enter "pi" (without the quotes) and press [Enter]. Enter "raspberry" as the password and [Enter].


At the next $ prompt enter "startx &" to start the windows environment.

Let's write C!

Use the mouse to open the menu (bottom left) Accessories/LXTerminal.


The mostly black window will open and you will be presented with the $ command line prompt, e.g.


Type "nano" [Enter] to open a simple text editor. This editor allows you to use the keyboard arrow keys to move around the file and pressing the [Ctrl] key with O (shown in the help as ^O) allows you to save any text you write in a file.

Enter the following into the editor, making sure you use the same case for the statements:


#include <stdio.h> 
  while (1) 
  printf("Steve was here! "); 


Then press ^O (Ctrl+O) and enter "hi.c" [Enter] as a filename. Press ^X to exit the editor.

Back at the $ prompt, type "ls" [Enter] to show the list of files in the current directory. You should see the new "hi.c" file listed. Type "gcc hi.c" [Enter]. Gcc is the compiler that will change the hi.c language text file into a binary executable that the Pi processor can execute.

If there are no typing mistakes in the hi.c file then gcc will not print anything to the screen. To see the output type 'ls' and [Enter]. You should see a file called a.out. a.out is the default output file for gcc.

To execute a.out type "./a.out" [Enter]


As you can see, the program will scroll the (annoying) message up the screen until you intervene to stop it. Press ^C to cause execution to stop.


Well done, that was your first C program on the Raspberry Pi.


Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


About Us

Bringing you the latest Raspberry Pi news, tutorials and user forum.

The Fruity Computer

  • Create New...