trackme21
Posts: 3
Joined: Mon Jul 08, 2013 9:01 pm

First c program write in raspberry pi.

Mon Jul 08, 2013 9:13 pm

I'm trying to write my first c program in RPi.
Is there any compiler or editor in RPi?
If not how can i write c program in RPi with command prompt.
Also how can i compile and run it!

Thanks in advance.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12585
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: First c program write in raspberry pi.

Mon Jul 08, 2013 9:17 pm

any text editor would do, but there are specialized ones with "syntax highlighting"
command line compilers like GCC are often still the power beneath the playful GUI's, but are often faster to use without them.
One of the best known, and best, is still the GNU compiler GCC.

User avatar
joan
Posts: 14658
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: First c program write in raspberry pi.

Mon Jul 08, 2013 9:24 pm

Create a file called myprog.c

Code: Select all

#include <stdio.h>

int main(int argc, char *argv[])
{
  if (argc > 1) printf("Your name is %s\n", argv[1]);
  else printf("Your name is not known\n");
}
Compile with

cc -o myprog myprog.c

Run with

$ ./myprog
Your name is not known
$ ./myprog ken
Your name is ken

isaias
Posts: 4
Joined: Wed Feb 20, 2013 3:40 am

Re: First c program write in raspberry pi.

Fri Jul 12, 2013 2:48 am

Hey Mahjongg,
Can you suggest please some editor for the RPI to write that C programm so it can check the sintax or something like that ?

Thanks
Regards

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12585
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: First c program write in raspberry pi.

Fri Jul 12, 2013 10:13 am

I don't have a personal preference, but there are many editors that would work.
Nano, and Vim are the first two that come to mind, but I think JED / XJED is more or less standard.
I would suggest trying all of these to see which you like best.

The standard "wordpad" like editor of raspbian is leafpad, it would work but has no syntax highlighting.

isaias
Posts: 4
Joined: Wed Feb 20, 2013 3:40 am

Re: First c program write in raspberry pi.

Fri Jul 12, 2013 4:40 pm

Thanks a lot for your info.
I will try nano.

Isaias

kghunt
Posts: 383
Joined: Sun Mar 04, 2012 9:28 am

Re: First c program write in raspberry pi.

Fri Jul 12, 2013 9:22 pm

I am using adafruit a web ide as I do not have X installed on my pi.

User avatar
rpdom
Posts: 16082
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: First c program write in raspberry pi.

Sat Jul 13, 2013 4:50 am

I use vi (nvi) on my Pi, or geany on my Debian x86 laptop for editing.

I would use geany on my Pi, but I don't use a desktop on there. Geany has some syntax highlighting and other features, but I mostly use it as a basic text editor.

User avatar
JRV
Posts: 270
Joined: Mon Apr 02, 2012 1:39 pm
Location: Minneapolis, MN

Re: First c program write in raspberry pi.

Fri Aug 02, 2013 3:07 pm

I use gedit for writing code, and I use this simple GUI that I wrote:

Code: Select all

#!/bin/sh
#
#Front end for GCC compiler.
#
# filename = C-GUI

WFLOADED=0

cd C

while [ $# -eq 0 ]
do
 
WORK=`cat WORKFILE.txt`
WORKC=$WORK".c"
WFLOADED=1

   CHOICE=`zenity --title="C-GPIO" --text="Work File:    "$WORK --height=400 --width=300 \
--list --column="Front End For GCC Compiler" "Create or Select Work FIle" "Edit    "$WORKC "Compile    "$WORKC "Execute    "$WORK "Terminal" "Exit"`

 #  clear

   if [ "$CHOICE" = "Create or Select Work FIle" ]; then
      clear
      TEMP=`zenity --entry --entry-text="Currently:  "$WORK --width=300`
      if [ $TEMP ]; then
         WORK=$TEMP
         WORKC=$WORK".c"
         WFLOADED=1
         echo $WORK > WORKFILE.txt 
     fi
   fi

   if [ "$CHOICE" = "Edit    "$WORKC ]; then
      if [ $WFLOADED -gt 0 ]; then
         gedit $WORK".c"
      else
         zenity --info --text="Work file is empty, create or select a work file." --timeout=2
      fi
   fi

   if [ "$CHOICE" = "Compile    "$WORKC ]; then
      if [ $WFLOADED -gt 0 ]; then
         clear
         cc -o $WORK $WORK.c
      else
         zenity --info --text="Work file is empty, create or select a work file." --timeout=2
      fi
   fi

   if [ "$CHOICE" = "Execute    "$WORK ]; then
      if [ $WFLOADED -gt 0 ]; then
         clear
         echo " " 
         sudo ./$WORK
         echo " "
         echo -n "Press <Enter> to continue: "
         read WORKHOLD
         clear
      else
         zenity --info --text="Work file is empty, create or select a work file." --timeout=2
      fi
   fi

   if [ "$CHOICE" = "Terminal" ]; then
      clear
      lxterminal
   fi

   if [ "$CHOICE" = "Exit" ]; then
      clear
      exit 0
   fi

done

Copy into a directory named "C", name it "C-GUI".
Make it executable with the command:

Code: Select all

chmod +x C-GUI
Create an empty file named "WORKFILE.txt"
The working file name is entered without the ".c" extension.

If you want to use gedit you will need to install it,
or you can change all instances of gedit to leafpad.

This script uses zenity to display the GUI.
I don't remember if zenity is installed by default or if I installed it.

To run open a terminal and type:

Code: Select all

C/C-GUI
Select "Create or Select Work File" and enter the filename (without the ".c")

s_erno
Posts: 9
Joined: Mon Jun 24, 2013 12:21 pm

Re: First c program write in raspberry pi.

Tue Aug 06, 2013 1:50 pm

Not sure how relevant this is now, but geany is really worth a try. Has coloring and such included, and I think it can even compile some stuff if configured right.

AndyInSurrey
Posts: 24
Joined: Mon Mar 05, 2012 8:45 am

Re: First c program write in raspberry pi.

Thu Aug 08, 2013 2:56 pm

I like working in the terminal on the RasPi and use nano as my text editor to write c and c++ programs. However, if you are a beginner you would be best advised to use an IDE to help, Geany is a good one on the RasPi, but would strongly advise that you install the debugger plugin for it, which will greatly assist you in understanding why your code isn't working the way you intend.

funk247
Posts: 11
Joined: Sun Mar 03, 2013 1:00 pm

Re: First c program write in raspberry pi.

Thu Aug 08, 2013 11:54 pm

You might wanna give code::blocks a try as well, its available from the Pi store and is pretty simple to use. Its got the sntax highlighting and limited code completion.

I've been using it quite a lot as it makes organizing things very easy and offers a relatively familiar GUI.

ejames
Posts: 15
Joined: Thu Feb 02, 2012 1:19 pm

Re: First c program write in raspberry pi.

Wed Aug 14, 2013 2:55 pm

vi or emacs are good choices
and learn to use makefiles asap
http://www.cprogramming.com/tutorial/makefiles.html

Manvir
Posts: 2
Joined: Thu May 15, 2014 2:56 pm

C programming in Raspbarry Pi

Thu May 15, 2014 3:02 pm

Hello,

I have started working on RPi two days ago. I have seen some tutorials to program RPi using Python but I do not have any experience in Python. I am very good at C. I do not have time to learn Python because I want to finish my project as soon as possible. I want to read the sensors using I2C. I have a 4 channel board to connect the sensors with the board.

Is it possible to read the sensors using C language only? Please help me if somebody has experience to read the GPIO of RPi using C.

Thanks. Waiting for you valuable comments.

User avatar
DougieLawson
Posts: 37074
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: First c program write in raspberry pi.

Fri May 16, 2014 6:33 am

What sensor and how is it interfaced?

http://wiringpi.com/ & http://abyz.co.uk/rpi/pigpio/ both have good libraries for I2C and SPI.
I've also used https://github.com/quick2wire/quick2wire-python-api because that works with my Q2W boards.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Manvir
Posts: 2
Joined: Thu May 15, 2014 2:56 pm

Re: First c program write in raspberry pi.

Fri May 16, 2014 10:08 am

Hello

Thanks for the reply.

I am using angle encoder, a pressure sensor, a voltage sensor and a current sensor. Because RPi hasnt enough GPIO to interface all these sensors, I have ordered one A/D board 4 channel. I can connect all the 4 sensors to this board and read them via I2C.

Thanks. Waiting for your reply.

User avatar
DougieLawson
Posts: 37074
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: First c program write in raspberry pi.

Fri May 16, 2014 10:14 am

Reading I2C with Gordon's WiringPi library is trivial. The only requirement with WiringPi is that the program runs UID(0), so you have to run it with sudo.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
R61zt
Posts: 65
Joined: Fri Feb 01, 2013 3:55 pm

Re: First c program write in raspberry pi.

Sat May 17, 2014 3:04 pm

You were asking which text editor to use so I'll write out every step which works from the prompt on a default raspian wheezy installation and also works in a terminal after startx has been called.

cd <yournewdirectory>
where <example> brackets indicate something which you must change to what you are using.

nano helloworld.c
opens a blank helloworld.c in one of the text editors
type in some c code to do something, preferably not printf-ing any rude words.
CTRL-O to save
CTRL-X to exit
that will return you to the prompt, where you may type
gcc -o helloworld hellowold.c
ls -l
to list what is there with details.
chmod +x helloworld
if ls-l did not find an "x" for "executable" against helloworld
note that executables might sometimes be coloured green to make them more noticeable from ls.
to run an executable named helloworld
./helloworld

what happens next will be whatever was asked for correctly in your program.

PeterRovman
Posts: 10
Joined: Mon Sep 11, 2017 5:41 pm

Re: First c program write in raspberry pi.

Fri Sep 15, 2017 4:33 pm

There is something missing (for me):
I wrote the program (using Geany) suggested by Joan but don't know where to save it. When I try the compile it says there is no such directory or file, no matter where I put it or how I specify the directory.
A lot of this post does not look like C that I have used. for example what does -o do, where do I find what $ ./ means ?

User avatar
DougieLawson
Posts: 37074
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: First c program write in raspberry pi.

Fri Sep 15, 2017 4:41 pm

~ means home directory./means here, current directory
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
B.Goode
Posts: 9243
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: First c program write in raspberry pi.

Fri Sep 15, 2017 5:03 pm

what does -o do
From man cc
-o file
Place output in file file. This applies regardless to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.
If -o is not specified, the default is to put an executable file in a.out, the object file for source.suffix in source.o, its assembler file in source.s, a precompiled header file in source.suffix.gch, and all preprocessed C source on standard output.

PeterRovman
Posts: 10
Joined: Mon Sep 11, 2017 5:41 pm

Re: First c program write in raspberry pi.

Fri Sep 15, 2017 5:36 pm

Thanks but it does not answer my question.
I assume I open the terminal under Accessories then type
cc -o cprog myprog.c
where is the Pi expecting to find the directory cprog? Should I have saved it in Home or Home\Pi or somewhere else?
No place seems to work. Where is the "command line"? Does this mean the Terminal?
What does -o mean? or is that a zero?
I also assume that cc invokes a C compiler but which one? Is there even one loaded?
I read the guide and RPi Basics but need some other reference to cover these basic steps.

The last post helps with the -o thanks.
Is this stuff I don't understand a Linux thing? It is totally strange to me.

User avatar
rpdom
Posts: 16082
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: First c program write in raspberry pi.

Fri Sep 15, 2017 5:43 pm

PeterRovman wrote:
Fri Sep 15, 2017 5:36 pm
where is the Pi expecting to find the directory cprog?
It isn't expecting to find a directory called cprog. It is going to write a file called cprog in the current directory. That will be your executable program.
Where is the "command line"? Does this mean the Terminal?
Yes, the "terminal" is where you type commands on a command line.
What does -o mean? or is that a zero?
It is a lower case "O", meaning "output file".
I also assume that cc invokes a C compiler but which one?
cc is a usually a link to the gcc compiler.

jahboater
Posts: 5027
Joined: Wed Feb 04, 2015 6:38 pm

Re: First c program write in raspberry pi.

Fri Sep 15, 2017 5:49 pm

PeterRovman wrote:
Fri Sep 15, 2017 5:36 pm
Thanks but it does not answer my question.
I assume I open the terminal under Accessories then type
cc -o cprog myprog.c

where is the Pi expecting to find the directory cprog? Should I have saved it in Home or Home\Pi or somewhere else?
What directory cprog? "cprog" is a file. In fact it is your new program, and it can go anywhere you like. For a simple hello world program, it might as well go in the current directory and you can run it with ./cprog
PeterRovman wrote:
Fri Sep 15, 2017 5:36 pm
No place seems to work. Where is the "command line"? Does this mean the Terminal?
What does -o mean? or is that a zero?
Please re-read carefully the previous post by B.Goode.
PeterRovman wrote:
Fri Sep 15, 2017 5:36 pm
I also assume that cc invokes a C compiler but which one? Is there even one loaded?
It will invoke GCC which comes already installed on Raspbian. It is a very good compiler and you can just use it straight away.
PeterRovman wrote:
Fri Sep 15, 2017 5:36 pm
I read the guide and RPi Basics but need some other reference to cover these basic steps.
You can read most beginner books on C which should help.
PeterRovman wrote:
Fri Sep 15, 2017 5:36 pm
Is this stuff I don't understand a Linux thing? It is totally strange to me.
Its basic command line stuff. Similar in fact to Windows or IOS at this level. So (at this level) its not a "Linux thing".

PeterRovman
Posts: 10
Joined: Mon Sep 11, 2017 5:41 pm

Re: First c program write in raspberry pi.

Fri Sep 15, 2017 6:01 pm

Thanks rpdom and jahboater. very helpful
It is all strange to me coming from MS Win, CCS "C", and MPLAB programming PICs.
Where do I look up command line instructions? A book on C? I don't remember seeing or using this in my C programming.

Edit:
Thanks everyone; I now have my first C program running on my RPi.
Here are Joan’s instructions with my comments added for real novices like me. I changed the file and folder names to prevent the confusion I had.

Click top left of the monitor (or bottom right depending on the version), move the cursor over Programming and click on Geany. This opens an editor. Type the following program into this editor:

#include <stdio.h>
int main(int argc, char *argv[])
{
if (argc > 1) printf("Your name is %s\n", argv[1]);
else printf("Your name is not known\n");
}

Select File, save as, create folder (top right) named cprogs and save the file as myprog.c. Close Geany. This is your source code which you need to compile into an executable program.
Click top left of the monitor, move the cursor over Accessories and select Terminal.

At the $ prompt type
CD cprogs (enter) // to get into the cprogs folder
cc –o newprog myprog.c // to compile the program as file named newprog. See previous post for details.

Now, still at the $ prompt, run your program. Type

./newprog (enter) // no spaces.
Then with your name
./newprog Fred
Last edited by PeterRovman on Fri Sep 15, 2017 7:41 pm, edited 1 time in total.

Return to “C/C++”