Hi Folks.
Just some feedback for all on the experience writing my first C code on Raspberry Pi.
I am a very experienced programmer and electronics engineer, but I have no experience with Linux, so its an interesting ride. Here is how it played out on day 1.
Getting the hardware up and running was OK with the downloaded image from the site. However I am on Mac OSX and I ended up having to manually enter the parameters into dd to write to the SD card (had never used dd before...). The script mentioned didnt work properly to do that. However, all good and everything booted OK eventually.
Next, I plugged in the Apple keyboard with its mouse connected into the keyboard. No Good. Seems like that's too much power. So connected the keyboard and mouse seperately. After a few tries that works OK although repeated keystrokes happen now and again. A lower powered keyboard and mouse are probably in order, dont really want to go to a powered hub just for that.
Next, my language of choice is straight procedural C, and I want to program the Raspberry PI using POSIX type code if possible.
I see that Geany is pre-installed, and I was delighted to find that it has a main.c template. So, make a new main.c, add "printf("Hello World\n");" compile button !! all good. Now, to run it........
Ah. Next snag. Geany is complaining that it "Could not find terminal "xterm", and advises me to "check path for terminal tool setting in Preferences". I guess it wants to run the program in a terminal.
So, I went to prefs and I pressed the button to reset that path to something else. Of course I have got no idea where a terminal app lives in linux..... After a bit of searching I discovered that these types of apps live in /usr/bin
And indeed I found something called "x-terminal-emulator" - selected that.
Now back to Hello World.c ! Compiles, and now it executes.....Result !
We are all trying to figure out what Raspberry Pi should be to different people and I am a child of the commodore PET / sinclair age. It's hard to know if learning computing like that is still a relevant experience, although I do worry that modern software engineering is way too 'dependent' on thousands of pre-requisites, like libraries and drivers. So, my mission is to see how close to the 'metal' it's practical to get and still get the PI to do something interesting for someone learning to program.
I did think my next project might be to make the Pi play some audio notes, connected to some GPI pins as keys. However, I can already see that to access the sound we are going through a mass of libraries. It almost makes you want to hook up a D-A Convertor and drive that.
Anyway. Day 1 for a linux newbie is going pretty well. It makes you realise how much you dont know about a new platform, and the required knowledge user manual is little thicker than the one which came with a BBC Micro...
I wonder whether it might end up being better to run an emulator for something like an 80's computer and just work in that environment for simple programming learning.
Overall, this is great fun. Can't wait to get something more substantial than Hello World up and running......
Re: First 'C' Hello World Program on Raspberry Pi
Thanks abstract, your post helped me get my first program running on my Pi
Since I got stuck this weekend without a USB keyboard I've had to get SSH working to fire up the UI remotely and then program just using a mouse ... copy-pasting one or two characters at a time. Hence my "Hello World" says "untitled".
Since I got stuck this weekend without a USB keyboard I've had to get SSH working to fire up the UI remotely and then program just using a mouse ... copy-pasting one or two characters at a time. Hence my "Hello World" says "untitled".
Re: First 'C' Hello World Program on Raspberry Pi
saskwoch said:
program just using a mouse ... copy-pasting one or two characters at a time. Hence my "Hello World" says "untitled".
Wow, that's a way to program I had not considered- the "ransom note" school of programming. Congratulations on your perseverance!
program just using a mouse ... copy-pasting one or two characters at a time. Hence my "Hello World" says "untitled".
Wow, that's a way to program I had not considered- the "ransom note" school of programming. Congratulations on your perseverance!
Re: First 'C' Hello World Program on Raspberry Pi
Thanks! I was annoyed with myself for thinking I had a USB keyboard, when I pulled the lead from my old Compaq Presario I was left looking dumbly at a PS/2 connection (duh!).
I guess I could have knocked up the code on my PC and transferred it via the SD card (since /boot is visible) and moved/copied it using the File Manager but it didn"t take long using the mouse.
I guess I could have knocked up the code on my PC and transferred it via the SD card (since /boot is visible) and moved/copied it using the File Manager but it didn"t take long using the mouse.
- Jim Manley
- Posts: 1600
- Joined: Thu Feb 23, 2012 8:41 pm
- Location: SillyCon Valley, California, and Powell, Wyoming, USA, plus The Universe
- Contact: Website
Re: First 'C' Hello World Program on Raspberry Pi
saskwoch said:
Thanks! I was annoyed with myself for thinking I had a USB keyboard, when I pulled the lead from my old Compaq Presario I was left looking dumbly at a PS/2 connection (duh!).
Are you aware that there are one or two dollar/pound PS/2-USB adapters that allow you to use whichever flavor of keyboard/mouse you have with whichever computer interface you need? One converts PS/2 plugs to USB, and another converts USB plugs to PS/2 - obviously, you need the former.
Thanks! I was annoyed with myself for thinking I had a USB keyboard, when I pulled the lead from my old Compaq Presario I was left looking dumbly at a PS/2 connection (duh!).
Are you aware that there are one or two dollar/pound PS/2-USB adapters that allow you to use whichever flavor of keyboard/mouse you have with whichever computer interface you need? One converts PS/2 plugs to USB, and another converts USB plugs to PS/2 - obviously, you need the former.
The best things in life aren't things ... but, a Pi comes pretty darned close! 
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!

"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!
Re: First 'C' Hello World Program on Raspberry Pi
Thanks Jim. You've reminded me that I do have a USB to PS/2 adapter so it figures that there'd be a PS/2 to USB adapter. However, I've not had much luck with the former working so I'd prefer to get a USB keyboard.
I took my RPi into work today and can confirm it works with a Logitech MK700/MK710 (M/N: Y-R0006) keyboard and M705 mouse via the tiny USB Bluetooth dongle (M/N: C-U0007), so I have something like that in mind now. It also works with an NEC M260X projector but I won't be going that far.
I took my RPi into work today and can confirm it works with a Logitech MK700/MK710 (M/N: Y-R0006) keyboard and M705 mouse via the tiny USB Bluetooth dongle (M/N: C-U0007), so I have something like that in mind now. It also works with an NEC M260X projector but I won't be going that far.
-
- Posts: 3
- Joined: Sun Jun 10, 2012 3:16 pm
Re: First 'C' Hello World Program on Raspberry Pi
Can You Please Help - I am following your instructions but do not know what to press in preferences (in the X on lxde, debian squeeze) - can you please help; as i really want to do some coding projects on the R-Pi.
Thanks
Thanks

iNetworker's Devices List:
1x iPod 2g 1x iPod 3g 1x iPhone 3GS 1x iPod Touch 4g 1x 24" iMac (early 2009) {4gb ram, nvidia geforce 9400 256mb 1x New MacBook Pro (Retina Display)
ALL DEVICES RUNNING IOS 5.1.1 (EXCEPT iPod 4G RUNNING IOS6) OR LION
1x iPod 2g 1x iPod 3g 1x iPhone 3GS 1x iPod Touch 4g 1x 24" iMac (early 2009) {4gb ram, nvidia geforce 9400 256mb 1x New MacBook Pro (Retina Display)
ALL DEVICES RUNNING IOS 5.1.1 (EXCEPT iPod 4G RUNNING IOS6) OR LION
Re: First 'C' Hello World Program on Raspberry Pi
Or, since you've already said you got SSH working, you could just knock up the code on your PC and copy it to your running RasPi using WinSCPsasqwoch wrote:I guess I could have knocked up the code on my PC and transferred it via the SD card (since /boot is visible) and moved/copied it using the File Manager but it didn"t take long using the mouse.

See one of http://www.raspberrypi.org/phpBB3/searc ... lxterminaliNetworker wrote:Can You Please Help - I am following your instructions but do not know what to press in preferences (in the X on lxde, debian squeeze) - can you please help; as i really want to do some coding projects on the R-Pi.
-
- Posts: 406
- Joined: Sun Nov 20, 2011 11:37 am
- Contact: Website
Re: First 'C' Hello World Program on Raspberry Pi
Hi abstract,
Thanks for your post.
I am very much a beginner at C - I am learning bits from a couple of free tutorials I have read and some great videos on YouTube of Prof Jerry Cain of Stanford.
I hope you'll post some more C code!
There's a section on here dedicated to C and C++.
I've also been reading Donald Knuth's "The Art of Computer Programming". Here's a little C code I wrote to do the "Euclid's Algorithm" he mentions early on. It seems to work pretty well, but if you have any advice / comments, I'd love to learn from someone more experienced.
Thanks for your post.
I am very much a beginner at C - I am learning bits from a couple of free tutorials I have read and some great videos on YouTube of Prof Jerry Cain of Stanford.
I hope you'll post some more C code!
There's a section on here dedicated to C and C++.
I've also been reading Donald Knuth's "The Art of Computer Programming". Here's a little C code I wrote to do the "Euclid's Algorithm" he mentions early on. It seems to work pretty well, but if you have any advice / comments, I'd love to learn from someone more experienced.
Code: Select all
#include <stdio.h>
int main(int argc, char argv[])
{
int m;
int n;
int temp;
int r;
printf("Euclid's Algorithm: Greatest Common Divisor.\n");
printf("Enter the first number: ");
scanf("%d", &m);
printf("Enter the second number: ");
scanf("%d", &n);
if (m < n)
{
temp = m;
m = n;
n = temp;
}
printf("Num 1: %d\n", m);
printf("Num 2: %d\n", n);
while (r != 0)
{
r = m % n;
if (r == 0)
{
printf("Answer: %d\n", n);
}
m = n;
n = r;
}
return 0;
}
Re: First 'C' Hello World Program on Raspberry Pi
Or again, and more sensibly, either :AndrewS wrote:Or, since you've already said you got SSH working, you could just knock up the code on your PC and copy it to your running RasPi using WinSCP :)sasqwoch wrote:I guess I could have knocked up the code on my PC and transferred it via the SD card (since /boot is visible) and moved/copied it using the File Manager but it didn"t take long using the mouse.
- Use a remote X session to do the programming on the pi, using your computer as a dumb graphic terminal (i.e use your computer's screen, keyboard and mouse)
- use a console mode editor, and do your programming over ssh.
Programming by mouse deserves some sort of prize for bloody minded perseverance, though. Well done.
Simon
Re: First 'C' Hello World Program on Raspberry Pi
Rather than checking the value of r twice for each loop, you could simply move the answer-printf to just before the return 0.antiloquax wrote:It seems to work pretty well, but if you have any advice / comments, I'd love to learn from someone more experienced.
Edit: Oh, and it would be a good idea to initialize r to a non-zero value before the while loop

(without initialization, variables contain random values, so if you're unlucky it could start out as 0)
-
- Posts: 406
- Joined: Sun Nov 20, 2011 11:37 am
- Contact: Website
Re: First 'C' Hello World Program on Raspberry Pi
Hi Andrew,
Thanks for reading my code!
Yes, I see that it is more elegant to do as you suggested.
mark
Thanks for reading my code!
Yes, I see that it is more elegant to do as you suggested.
mark
-
- Posts: 14031
- Joined: Fri Mar 09, 2012 7:36 pm
- Location: Vallejo, CA (US)
Re: First 'C' Hello World Program on Raspberry Pi
I'm not only using my RPi with PS/2 mouse and keyboard, but also a VGA monitor (Neewer adpator) all running through a KVM. It probably helps a bunch that the KVM is powered, so it shouldn't be drawing any power from the RPi to power either keyboard or mouse.saskwoch wrote:Thanks Jim. You've reminded me that I do have a USB to PS/2 adapter so it figures that there'd be a PS/2 to USB adapter. However, I've not had much luck with the former working so I'd prefer to get a USB keyboard.
- Jim Manley
- Posts: 1600
- Joined: Thu Feb 23, 2012 8:41 pm
- Location: SillyCon Valley, California, and Powell, Wyoming, USA, plus The Universe
- Contact: Website
Re: First 'C' Hello World Program on Raspberry Pi
It never occurred to me that a powered KVM could be used to provide power to the keyboard and mouse in lieu of a hub, in addition to keeping them available to the computer to which they were originally connected. Great point! I'm so used to remote logins in my daily work on servers and network-distributed systems that I haven't used a KVM since before the advent of USB keyboards/mice.W. H. Heydt wrote:I'm not only using my RPi with PS/2 mouse and keyboard, but also a VGA monitor (Neewer adpator) all running through a KVM. It probably helps a bunch that the KVM is powered, so it shouldn't be drawing any power from the RPi to power either keyboard or mouse.
I predict this-here Internet thingy could actually prove to be useful one of these days ...

The best things in life aren't things ... but, a Pi comes pretty darned close! 
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!

"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!
Re: First 'C' Hello World Program on Raspberry Pi
Hi.Maybe trivial, but I'm not able to run debug plugin in geany.
Even more, i cannot see the correct debug panel (see following image) still i get an error for terminal after setting the /usr/bin/x-terminal-emulator.
Any suggestion?
Even more, i cannot see the correct debug panel (see following image) still i get an error for terminal after setting the /usr/bin/x-terminal-emulator.
Any suggestion?
- Attachments
-
- immagine 1.gif (53.09 KiB) Viewed 14523 times
-
- Immagine 3.gif (10.92 KiB) Viewed 14527 times
-
- Immagine 2.gif (31.22 KiB) Viewed 14527 times
-
- Posts: 2
- Joined: Sat Jun 30, 2012 5:39 pm
Re: First 'C' Hello World Program on Raspberry Pi
Hi all, I know I'm a but late to the party but I'm actually retiring a tutorial series on programming in C with Geany. I've also written a piece on graphics over ssh (X11 tunnelling). I'd love to se the result of your audio project if it comes about.
If you're interested, I'm writing over at http://www.thepiuser.co.uk. check it out!
If you're interested, I'm writing over at http://www.thepiuser.co.uk. check it out!
Re: First 'C' Hello World Program on Raspberry Pi
This was an good read, Abstract. I find it especially interesting because of your point of view as an IT expert but also a Linux newbie.
Re: First 'C' Hello World Program on Raspberry Pi
Geany: personal experience:
- I cannot let geany work properly in debug mode (no debug window is active)
- I still have the well known problem with x10 terminal unfixed (despite many temptatives suggested by web)
QTcreator:
- started at first time, debug is workin
- still some problems with x10 terminal (same as geany)
- I cannot let geany work properly in debug mode (no debug window is active)
- I still have the well known problem with x10 terminal unfixed (despite many temptatives suggested by web)
QTcreator:
- started at first time, debug is workin
- still some problems with x10 terminal (same as geany)
Re: First 'C' Hello World Program on Raspberry Pi
I rarely even have a keyboard connected to my Pi, everything is done via ssh.
Here's a complete idiot's guide to doing a hello world c program remotely on the pi:
- download putty from http://www.putty.org/
- run it and type in ip address of the Pi
- click open
- Login with normal user/password
nano hello.cctrl+x to exit and press y<enter> to save
gcc hello.c -o hello
./hello
hello world of pi
Here's a complete idiot's guide to doing a hello world c program remotely on the pi:
- download putty from http://www.putty.org/
- run it and type in ip address of the Pi
- click open
- Login with normal user/password
nano hello.c
Code: Select all
#include <stdio.h>
int main()
{
printf("hello world of pi \n");
}
gcc hello.c -o hello
./hello
hello world of pi
Android app - Raspi Card Imager - download and image SD cards - No PC required !
Re: First 'C' Hello World Program on Raspberry Pi
thanks for all the handy info guys! i'm currently learning to program at university and the RPi seems like it will be great to play around with, maybe see if i can make a little game for it or something. Eagerly awaiting the arrival of mine (hopefully today)
-
- Posts: 49
- Joined: Mon Mar 05, 2012 11:48 am
Re: First 'C' Hello World Program on Raspberry Pi
Thanks Mike, this is exactly what I needed. I saw your post several days ago and after much searching and with only a vague memory of the content I've only just found it again.mikerr wrote:I rarely even have a keyboard connected to my Pi, everything is done via ssh.
Here's a complete idiot's guide to doing a hello world c program remotely on the pi:
- download putty from http://www.putty.org/
- run it and type in ip address of the Pi
- click open
- Login with normal user/password
nano hello.cctrl+x to exit and press y<enter> to saveCode: Select all
#include <stdio.h> int main() { printf("hello world of pi \n"); }
gcc hello.c -o hello
./hello
hello world of pi
Perfect.
Re: First 'C' Hello World Program on Raspberry Pi
Hi I am interested in debugging C code using the Geany IDE. Maybe your tutorials cover that? But the link you gave does not seem to be working at present. It gives the message: "Error establishing a database connection". Is there a new link ?
Cheers,
Steven
Cheers,
Steven
RomeoPapacy wrote:Hi all, I know I'm a but late to the party but I'm actually retiring a tutorial series on programming in C with Geany. I've also written a piece on graphics over ssh (X11 tunnelling). I'd love to se the result of your audio project if it comes about.
If you're interested, I'm writing over at http://www.thepiuser.co.uk. check it out!
"The time has come," the Walrus said,
"To talk of many things..."
"To talk of many things..."