tech-mech
Posts: 116
Joined: Sun Feb 23, 2014 9:51 pm
Location: Portland, Oregon USA
Contact: Yahoo Messenger

Learning C / C++

Thu Feb 25, 2016 11:58 pm

I would like to learn to program my Pi with C / C++.

Is there a list of the included Libraries and what they do / contain?

A pdf would be very nice. How can I program if i don't know what commands are available?

Thanks

User avatar
PeterO
Posts: 5451
Joined: Sun Jul 22, 2012 4:14 pm

Re: Learning C / C++

Fri Feb 26, 2016 7:35 am

Google will find you several C tutorials. http://www.cprogramming.com/tutorial/c-tutorial.html comes near the top of the results. If that doesn't suit you, try one of the others,

As for libraries, there are lots ! :-) Rather than trying to find out about all of them, I suggests you choose a project and then look (google) for appropriate ones to your project.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
Pwl2
Posts: 22
Joined: Mon Mar 23, 2015 7:04 am
Location: Warwickshire

Re: Learning C / C++

Sat Feb 27, 2016 6:55 pm

I would recommend a book - Exercises for Programmers by Brian P. Hogan. It is language neutral as it simply sets 57 'tasks' from 'say Hello' to writing 5 full programs (ToDo list etc). After that I would pick a smallish program you will use - a 'pillow project', and code that. Coding something that you want is a great driver to fight language subtleties, learn defensive code techniques etc, etc. Unfortunately the book is only available in a dead tree edition AFAIK.

Hope this helps.

Paul
Pwl2

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Learning C / C++

Sat Feb 27, 2016 7:15 pm

tech-mech wrote:A pdf would be very nice. How can I program if i don't know what commands are available?
There has never been a time in the history of the universe where more information about programming has been available to all as easily as it is today. Rejoice! Just google C standard library and you'll get a ton to read: https://en.wikipedia.org/wiki/C_standard_library

ejolson
Posts: 4273
Joined: Tue Mar 18, 2014 11:47 am

Re: Learning C / C++

Sat Feb 27, 2016 8:13 pm

tech-mech wrote:I would like to learn to program my Pi with C / C++.

Is there a list of the included Libraries and what they do / contain?

A pdf would be very nice. How can I program if i don't know what commands are available?

Thanks
As C and to a certain extent C++ serve as the systems programming languages for Linux, almost every available library and function is available. Having too many libraries can be overwhelming: there are multiple libraries for sorting, linear algebra, FFTs and other algorithms as well as incompatible libraries for building mouse interfaces and three dimensional graphics applications. In comparison Java, Visual C# and Free Pascal make things easier by providing similar functionality through more limited choices available in standard libraries.

The updated editions of the original book by Kernighan and Ritchie "The C Programming Language" is still very good because of its clarity and not being overstuffed with irrelevant examples of poor programming style. This book focuses on writing command line programs, which avoids lots of unnecessary complexity. While many useful tasks are better done without a custom designed mouse interface, things like writing a video game would require reading other resources as well. Even if creating mouse interfaces and graphics is your ultimate goal, I would recommend starting simple by writing a number of command line programs first.

User avatar
PeterO
Posts: 5451
Joined: Sun Jul 22, 2012 4:14 pm

Re: Learning C / C++

Sat Feb 27, 2016 8:48 pm

ejolson wrote:The updated editions of the original book by Kernighan and Ritchie "The C Programming Language" is still very good because of its clarity and not being overstuffed with irrelevant examples of poor programming style.
K&R +1

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

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

Re: Learning C / C++

Sun Feb 28, 2016 7:03 pm

tech-mech wrote:I would like to learn to program my Pi with C / C++.

Is there a list of the included Libraries and what they do / contain?

A pdf would be very nice. How can I program if i don't know what commands are available?

Thanks
Good for you, the Pi is a great platform for learning.
Because there is a vast number of library functions and programming tools available, your are best deciding what you want to do first.

On the system itself is a set of manual pages.

Code: Select all

man strcpy
for example will tell you all you need about the string copy function.
You will likely not know what the function is called. Either try google, or try "apropos" or "man -k" which searches the short descriptions and manual page names for the string you give.

Code: Select all

man -k string
produces a long list of string related stuff from which you can refine your search. The manual is in sections and the number in brackets after the name gives the section number.
You need this if the name is in more than one section. Unsurprisingly see "man man" for details.
"We are in the beginning of a mass extinction, and all you can talk
about is money and fairy tales of eternal economic growth."
- Greta Thunberg

yodermk
Posts: 35
Joined: Sat Mar 07, 2015 9:54 pm

Re: Learning C / C++

Sun Feb 28, 2016 9:28 pm

I would first ask yourself why you would need plain C. If there is no good answer, please do yourself a favor and skip it and go straight to C++. Bjarne Stroustroup himself does not recommend learning C first. The only exception is if you really need C. And I think the only reason for needing C is if you're working on an old codebase that is in C. Of course there's a lot of that, from the Linux kernel to the Apache webserver to the Postgres database, etc etc. If you're looking to get involved in something like that then great! But in my opinion there's little reason to start a new project without taking advantage of C++11/14 features.

As for libraries, of course there's the C++ standard library which can do all kinds of abstract things. All kinds of other libraries are included in the Raspian repository, for things like databases, GUI, math/science, whatever. It's an endless world of opportunity!

User avatar
patrikg
Posts: 168
Joined: Sun Mar 18, 2012 10:19 pm

Re: Learning C / C++

Sun Feb 28, 2016 9:47 pm

Bucky at thenewboston has lots of tutorials. for programming.
He's pretty funny, when he teaches.
https://thenewboston.com/videos.php?cat=16

ejolson
Posts: 4273
Joined: Tue Mar 18, 2014 11:47 am

Re: Learning C / C++

Mon Feb 29, 2016 5:58 pm

yodermk wrote:I would first ask yourself why you would need plain C. If there is no good answer, please do yourself a favor and skip it and go straight to C++. Bjarne Stroustroup himself does not recommend learning C first. The only exception is if you really need C. And I think the only reason for needing C is if you're working on an old codebase that is in C. Of course there's a lot of that, from the Linux kernel to the Apache webserver to the Postgres database, etc etc. If you're looking to get involved in something like that then great! But in my opinion there's little reason to start a new project without taking advantage of C++11/14 features.
I suspect the Linux kernel is written in C rather than C++ because Linus felt C is better for writing an operating system kernel. Interestingly, many years later he designed and developed a source management system called git in C as well.

Quoting from Robert Pike who has been involved with Unix from the beginning at Bell Labs and currently works at Google with Brian Kernighan.
Rob Pike wrote:A couple of years ago, several of us at Google became a little frustrated with the software development process, and particularly using C++ to write large server software. We found that the binaries tended to be much too big. They took too long to compile. And the language itself, which is pretty much the main system software language in the world right now, is a very old language. A lot of the ideas and changes in hardware that have come about in the last couple of decades haven’t had a chance to influence C++. So we sat down with a clean sheet of paper and tried to design a language that would solve the problems that we have: we need to build software quickly, have it run well on modern multi-core hardware and in a network environment, and be a pleasure to use.
The new programming language he refers to is called golang or just go. While it is nice in many ways, it is not as central as C or even C++ on Linux. At the present time, I suspect the only reason someone would learn golang before C is by having an over enthusiastic computer science teacher.

The C programming language is simple enough that the entire language can be mastered by many in less than a year. It appears to me that C++ is so complicated that most people who use it haven't mastered the entire language. From what I remember, the main intention of C++ was to allow code from non-cooperating programmers to be used in the same project. While open source has increased the level of cooperation remarkably, for very large software projects the additional features of C++ can help.

Good luck with your learning project. Feel free to check back with questions and progress.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: Learning C / C++

Mon Feb 29, 2016 9:39 pm

giving hints about "learning C" is like giving hints about "learning a foreign language most different from anything what you knew or read or heard about before, perhaps Hopi or isiXhosa" . Or perhaps German, for probably 99% of the world's citizens ;).


I was learning basics (0.5% of my skills) about C/C++ by tutorials and books about Borland Turbo C and C++ Builder, and much of the rest I know by programming Arduinos, using the very illustrative Arduino Playground tutorials (relatively 99.5% of my skills, about 0.1% of overall-C ressources).

edit, I stand corrected: just 99.4%, 0.1% was by struggling with C for Raspi. :o

My hint:
start learning C on a far simpler platform. i.e., e.g., Arduinos, because there are lot of excellent links and examples and tutorials.
For Rasperry Pi, I am dispairing too, FTM, for lack of excellent links and examples and tutorials .... and even ressources, because most of driver libs are not available for C, but just for that crappy Python....

ps.
if you (or your browser) is capable of German,....
I wrote a Quick Guide for programming the Pi in C, for bloody dummies....:

Quick Guide für Raspberry Pi: C/C++ mit Geany für Dummies
http://www.mindstormsforum.de/viewtopic ... 689#p67768

but it's more about the setup of the ressources, and only little about actually programming...


... and the rest is: Learning by doing!

BTW: Here's an excellent C/C++ language reference ressource:
http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/reference/clibrary/

HTH!
Last edited by davenull on Mon Feb 29, 2016 10:28 pm, edited 2 times in total.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
PeterO
Posts: 5451
Joined: Sun Jul 22, 2012 4:14 pm

Re: Learning C / C++

Mon Feb 29, 2016 9:59 pm

davenull wrote: My hint:
start learning C on a far simpler platform. i.e., e.g., Arduinos,
The drawback with learning on an Arduino is that is not a standard platform with lots of libraries and facilities missing. No filesystem or networking or display to learn about. And no way to learn about using all the tools that support C/C++ programming like make.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: Learning C / C++

Mon Feb 29, 2016 10:02 pm

but you will learn the basic C syntax on Arduino, which is indispensible and which will be crucial frustrating for a newbie if you'd have to fight and struggle with all that crappy Linux stuff on Raspi before you might even get your first "Hello World" starting....

Arduino is:
load the IDE,
start programming,
upload,
and it works, out of the box!

which is finally the reason for the world-wide tremendous success of the Arduino concept!
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

yodermk
Posts: 35
Joined: Sat Mar 07, 2015 9:54 pm

Re: Learning C / C++

Fri Mar 04, 2016 2:05 am

Regarding C++ being too complex to master in a reasonable time, perhaps; but remember that it's designed so that you don't have to use every feature, and you don't pay for (in terms of performance and memory) what you don't use.

If nothing else, you should use C++ style I/O and things like auto variables and smart pointers because it's just a better way of doing things than you have in C. You don't have to use OOP, or write your own templates, or whatever.

Some people (like Linux) keep using C because they know it like the back of their hand. That doesn't mean it's the best thing to start with. A kernel, if you started writing it today, could absolutely be done in C++. It's designed for that kind of thing. Again, don't overcomplicate it. You don't have to use all the features of the language. Use what you understand and what makes sense in the specific engineering concerns of your project. Often that might not be much more than C, but just by using the C++ compiler you can still get some big benefits.

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6183
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: Learning C / C++

Fri Mar 04, 2016 10:46 am

yodermk wrote:Regarding C++ being too complex to master in a reasonable time, perhaps; but remember that it's designed so that you don't have to use every feature, and you don't pay for (in terms of performance and memory) what you don't use.

If nothing else, you should use C++ style I/O and things like auto variables and smart pointers because it's just a better way of doing things than you have in C. You don't have to use OOP, or write your own templates, or whatever.

Some people (like Linux) keep using C because they know it like the back of their hand. That doesn't mean it's the best thing to start with. A kernel, if you started writing it today, could absolutely be done in C++. It's designed for that kind of thing. Again, don't overcomplicate it. You don't have to use all the features of the language. Use what you understand and what makes sense in the specific engineering concerns of your project. Often that might not be much more than C, but just by using the C++ compiler you can still get some big benefits.
While I was at uni (not that long ago), I was working on relatively simple firmware for a device. I kept running out of space and memory. The firmware was very simple... read a few inputs, print a few things on the screen, send some things over the network. I then rewrote it in C, pretty much as it was (change cout to printf and so on). Suddenly, the memory footprint and program size dropped significantly.

A counter-example is that I was working on a simple GUI toolkit and found myself implementing C++ features in C, then shelved the project to redo it in C++ later. So now I default to C and only consider switching to C++ when the task is inherently OO-friendly.

What am I missing? Why did I have problems with it in the past if I should be seeing better performance simply by using a C++ compiler?

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: Learning C / C++

Fri Mar 04, 2016 11:31 am

what did I miss, who was opposing to use a C++ compiler?

I am also using gpp and using C++ libs, but myself I'm just writing ANSI C syntax, both on Raspberry and Arduino (which is also always C++ based).

But my point actually is a different one:

for a complete beginner a Raspi is too hard and too frustrating to start with in order to learn C/C++, because of all the underlying Linux stuff.
OTOH, on Arduino you might learn the basics of C/C++ a thousand times more easy, as you can focus on your program and don't have to struggle with OS issues.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Learning C / C++

Fri Mar 04, 2016 12:05 pm

davenull wrote:what did I miss, who was opposing to use a C++ compiler?

I am also using gpp and using C++ libs, but myself I'm just writing ANSI C syntax, both on Raspberry and Arduino (which is also always C++ based).

But my point actually is a different one:

for a complete beginner a Raspi is too hard and too frustrating to start with in order to learn C/C++, because of all the underlying Linux stuff.
OTOH, on Arduino you might learn the basics of C/C++ a thousand times more easy, as you can focus on your program and don't have to struggle with OS issues.
BASIC, anyone? ;-)

-Gordon
--
Gordons projects: https://projects.drogon.net/

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: Learning C / C++

Fri Mar 04, 2016 12:09 pm

perhaps, if you love spaghetti... ;)
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Learning C / C++

Fri Mar 04, 2016 12:11 pm

davenull wrote:perhaps, if you love spaghetti... ;)
You are mistaking BASIC with bad programming practices.

Show me the Italian pasta here:

Code: Select all

// factorial:
//    Recursive factorial generator
//    Demonstration of number formatting and recursion


numformat (15, 0)

for i = 1 TO 16 cycle
  print i;  "! = ";  fn factorial(i)
repeat
end

def fn factorial (n)
if n = 1 then
  = 1
else
  = n * fn factorial(n - 1)
endif
-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Learning C / C++

Fri Mar 04, 2016 12:13 pm

davenull wrote:for a complete beginner a Raspi is too hard and too frustrating to start with in order to learn C/C++, because of all the underlying Linux stuff.
OTOH, on Arduino you might learn the basics of C/C++ a thousand times more easy, as you can focus on your program and don't have to struggle with OS issues.
I don't see that.

If your program is command-line only, then RaspPi is much more friendly than the Arduino where you need a non-C-standard library for every bit of interaction.

Code: Select all

#include <stdio.h>

main()
 {
    printf("hello world!\n");
}
The command-line offers richer interaction than switches and lights, or even gyros and acceleration sensors, albeit it is maybe not as exciting and engaging.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: Learning C / C++

Fri Mar 04, 2016 12:19 pm

well, as already stated above:
giving hints about "learning C" is like giving hints about "learning a foreign language most different from anything what you knew or read or heard about before, perhaps Hopi or isiXhosa" .
or:

ask two programmers and you'll get three answers.

(and command lines are killing me... :twisted: )
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
PeterO
Posts: 5451
Joined: Sun Jul 22, 2012 4:14 pm

Re: Learning C / C++

Fri Mar 04, 2016 12:20 pm

I do wish people wouldn't post that poor version of hello.c It doesn't compile cleanly !

Code: Select all

gcc -Wall hello.c 
hello.c:3:1: warning: return type defaults to ‘int’ [-Wreturn-type]
 main()
 ^
hello.c: In function ‘main’:
hello.c:6:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
It should be ,,,,

Code: Select all

#include <stdio.h>

int main(void)
{
     printf("Hello World!\n");
     return 0;
}
which compiles cleanly !

Or my prefered version :

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
     printf("Hello World!\n");
     exit(EXIT_SUCCESS);
}
And don't get me started on code that doesn't parse the command line ! :D

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: Learning C / C++

Fri Mar 04, 2016 12:24 pm

as I stated:
never start learning C(++) on a Raspi....

nothing left to say... :twisted:
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Learning C / C++

Fri Mar 04, 2016 12:57 pm

davenull wrote:as I stated:
never start learning C(++) on a Raspi....

nothing left to say... :twisted:
Arduino provides you with a nice cozy little closed-room type environment. I do not believe most people actually learn much programming skills using it as-such. You learn how to join the dots on a myriad of pre-build librarys, classes and so on. But what if you want to write your own library/class?

Then you need to learn to program.

This is where you're at right now.

Personally, I'd suggest that the command-line under Linux is an easy place to start, however there is all this getting to grips with the command-line in the first place, but once you're there, you have the wealthiest of programming environments at your fingertips.

-Gordon
--
Gordons projects: https://projects.drogon.net/

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: Learning C / C++

Fri Mar 04, 2016 1:38 pm

command line under Linux, yes:

g++ -Wall -I/opt/vc/include -I/opt/vc/include/interface/vmcs_host/linux -I/opt/vc/include/interface/vcos/pthreads -o myfile.c myfile -pthread -lshapes -L/opt/vc/lib -lOpenVG -lEGL -lrt -lwiringPi -lpigpio

my goal actually is to focus on programs, not to struggle with the OS.
So I could focus on establishing the world-wide first autonomous recurrent neural multilayer net on Arduino (a Jordan net), after 1 month Arduino programming practice 8-)
(I admit that I already established the world-wide first autonomous neural single layer net on Lego NXT before, written in NXC, so I had a little experience in the world of "Not-eXactly" :) )


On Raspi instead, it's why I am where I am.

Thank Heaven, and thanks to you Gordon, there is eventually wiringPi to fake wiring Arduino ;)
without quite knowing about that, I had almost already abandoned all that Raspi C - and the whole Raspi thing - 5 months ago... :roll:
Last edited by davenull on Fri Mar 04, 2016 2:10 pm, edited 1 time in total.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

Return to “C/C++”