abstract
Posts: 3
Joined: Sat May 05, 2012 2:10 pm

Re: First 'C' Hello World Program on Raspberry Pi

Sat May 05, 2012 3:32 pm

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......

saskwoch
Posts: 8
Joined: Mon May 07, 2012 6:50 pm

Re: First 'C' Hello World Program on Raspberry Pi

Mon May 07, 2012 7:58 pm

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".

User avatar
jbeale
Posts: 3578
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: First 'C' Hello World Program on Raspberry Pi

Mon May 07, 2012 8:33 pm

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!

saskwoch
Posts: 8
Joined: Mon May 07, 2012 6:50 pm

Re: First 'C' Hello World Program on Raspberry Pi

Mon May 07, 2012 9:34 pm

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.

User avatar
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

Tue May 08, 2012 1:48 am

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.
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"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!!!

saskwoch
Posts: 8
Joined: Mon May 07, 2012 6:50 pm

Re: First 'C' Hello World Program on Raspberry Pi

Tue May 08, 2012 4:31 pm

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.

iNetworker
Posts: 3
Joined: Sun Jun 10, 2012 3:16 pm

Re: First 'C' Hello World Program on Raspberry Pi

Sun Jun 10, 2012 3:19 pm

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 :D
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

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: First 'C' Hello World Program on Raspberry Pi

Sun Jun 10, 2012 11:58 pm

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.
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 :)
iNetworker 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.
See one of http://www.raspberrypi.org/phpBB3/searc ... lxterminal

antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

Re: First 'C' Hello World Program on Raspberry Pi

Mon Jun 11, 2012 5:41 am

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.

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;
}

tufty
Posts: 1456
Joined: Sun Sep 11, 2011 2:32 pm

Re: First 'C' Hello World Program on Raspberry Pi

Mon Jun 11, 2012 5:54 am

AndrewS wrote:
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.
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 :)
Or again, and more sensibly, either :

- 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

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: First 'C' Hello World Program on Raspberry Pi

Mon Jun 11, 2012 11:59 am

antiloquax wrote:It seems to work pretty well, but if you have any advice / comments, I'd love to learn from someone more experienced.
Rather than checking the value of r twice for each loop, you could simply move the answer-printf to just before the return 0.

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)

antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

Re: First 'C' Hello World Program on Raspberry Pi

Mon Jun 11, 2012 1:26 pm

Hi Andrew,
Thanks for reading my code!
Yes, I see that it is more elegant to do as you suggested.
mark

W. H. Heydt
Posts: 11433
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: First 'C' Hello World Program on Raspberry Pi

Mon Jun 11, 2012 5:02 pm

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.
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.

User avatar
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

Mon Jun 11, 2012 5:34 pm

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.
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.

I predict this-here Internet thingy could actually prove to be useful one of these days ... :D
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"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!!!

brunialti
Posts: 17
Joined: Sat Jun 09, 2012 9:11 am

Re: First 'C' Hello World Program on Raspberry Pi

Tue Jun 19, 2012 5:20 pm

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?
Attachments
immagine 1.gif
immagine 1.gif (53.09 KiB) Viewed 13701 times
Immagine 3.gif
Immagine 3.gif (10.92 KiB) Viewed 13705 times
Immagine 2.gif
Immagine 2.gif (31.22 KiB) Viewed 13705 times

RomeoPapacy
Posts: 2
Joined: Sat Jun 30, 2012 5:39 pm

Re: First 'C' Hello World Program on Raspberry Pi

Fri Jul 06, 2012 10:21 pm

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!

Borat
Posts: 26
Joined: Mon Jun 11, 2012 4:17 pm

Re: First 'C' Hello World Program on Raspberry Pi

Sat Jul 14, 2012 11:56 pm

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.

brunialti
Posts: 17
Joined: Sat Jun 09, 2012 9:11 am

Re: First 'C' Hello World Program on Raspberry Pi

Sun Jul 15, 2012 4:03 pm

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)

mikerr
Posts: 2804
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: First 'C' Hello World Program on Raspberry Pi

Sun Jul 15, 2012 4:38 pm

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.c

Code: Select all

#include <stdio.h>
int main()
{
printf("hello world of pi \n");
}
ctrl+x to exit and press y<enter> to save

gcc hello.c -o hello
./hello
hello world of pi
nano.png
nano.png (12.55 KiB) Viewed 12867 times
Android app - Raspi Card Imager - download and image SD cards - No PC required !

Syrioth
Posts: 7
Joined: Fri Jul 27, 2012 9:49 am
Location: Scotland

Re: First 'C' Hello World Program on Raspberry Pi

Fri Jul 27, 2012 10:10 am

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)

Raspberry wino
Posts: 49
Joined: Mon Mar 05, 2012 11:48 am

Re: First 'C' Hello World Program on Raspberry Pi

Tue Jul 31, 2012 5:16 pm

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.c

Code: Select all

#include <stdio.h>
int main()
{
printf("hello world of pi \n");
}
ctrl+x to exit and press y<enter> to save

gcc hello.c -o hello
./hello
hello world of pi
nano.png
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.

Perfect.

User avatar
StevenP
Posts: 56
Joined: Tue Aug 14, 2012 7:55 pm
Location: Germany / UK

Re: First 'C' Hello World Program on Raspberry Pi

Fri Jan 25, 2013 6:56 am

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
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..."

Return to “General discussion”