Hi,
Just wondering for a beginner like me, why C and C++ are always in the same thread? Does this mean all the C code even the basic will work on C++ or vise versa?
I really want to learn C++ but all i can read here is about C compiling when it comes on GPIO. I know some basic of how C++ work but still cant figure out how to use GPIO on codeblock or qtcreator.
If it is ok can someone please give an example here a simple blinking LED code using C++ code and how to do you run it? thanks
btw, i tried to search in google/youtube about raspberry C++ but always come up to c code. a Sample.c file always shows up not Sample.cpp
Re: C++
C++ grew out of C. In the early days the C++ compiler generated C code which was then compiled by the C compiler. C++ is an object oriented language whereas C is procedural.
Because of the way C++ has developed the C language is an almost complete subset of C++. For most purposes a C program can be considered a C++ one but not vice versa.
C is still used to write many libraries because both C and C++ programs can use a C library. A C program cannot use a C++ library.
Because of the way C++ has developed the C language is an almost complete subset of C++. For most purposes a C program can be considered a C++ one but not vice versa.
C is still used to write many libraries because both C and C++ programs can use a C library. A C program cannot use a C++ library.
Re: C++
C++ is mostly an extension of C. The main things to watch out for.
1: when calling a C library from C++ code or writing C++ code that will be called by C code you have to use "extern "C"" blocks to tell the compiler it should use C calling conventions and no name mangling. For many libraries the supplied headers will do this for you.
2: C++ is stricter on type compatibility than C. This may mean you need more typecasts.
1: when calling a C library from C++ code or writing C++ code that will be called by C code you have to use "extern "C"" blocks to tell the compiler it should use C calling conventions and no name mangling. For many libraries the supplied headers will do this for you.
2: C++ is stricter on type compatibility than C. This may mean you need more typecasts.
Re: C++
Much (most) systems software is written in C including Raspbian (Linux) itself and most of the utilities. It is the natural language for this sort of thing.
While C++ is a very powerful and complex language, for a blinking LED it offers little or no advantage over C.
C++ was originally called "C with Classes". Of course it has evolved a lot since then, but it is still easy (and getting easier) to compile well written C with a C++ compiler, noting the caveats mentioned in the posts above.
Perhaps study the "C" GPIO example code. For example, look at the pigpio library. There is, on the pigpio website a file called "minimal_gpio.c" which is not too difficult to understand.
Maybe then you can decide: use a C library, or, if your application is simple enough, write it all directly in C++.
While C++ is a very powerful and complex language, for a blinking LED it offers little or no advantage over C.
C++ was originally called "C with Classes". Of course it has evolved a lot since then, but it is still easy (and getting easier) to compile well written C with a C++ compiler, noting the caveats mentioned in the posts above.
Perhaps study the "C" GPIO example code. For example, look at the pigpio library. There is, on the pigpio website a file called "minimal_gpio.c" which is not too difficult to understand.
Maybe then you can decide: use a C library, or, if your application is simple enough, write it all directly in C++.
Pi4 8GB (Raspberry Pi OS 64-bit), Pi4 4GB, Pi4 2GB, Pi1 Rev 1 256MB, Pi Zero
Re: C++
Wow thanks you guys for the quick reply.
However, can someone please post a sample of C and C++ program like a simple LED blinking. Those library still hard for me to read it. Sorry a super newbie, all i know in C++ is to use a variable and If else statement right now and i want to use this simple command and practice it with the GPIO.
I already download a lot of video tutorial that use C in GPIO but not C++ code. I just want an example how to turn ON and OFF the GPIO using C++.
EDIT:
I use "sudo python programName.py to run a simple blinking LED using python language.
How to run C++ or the .cpp in terminal? Is it sudo Cplusplus programName.cpp?
However, can someone please post a sample of C and C++ program like a simple LED blinking. Those library still hard for me to read it. Sorry a super newbie, all i know in C++ is to use a variable and If else statement right now and i want to use this simple command and practice it with the GPIO.
I already download a lot of video tutorial that use C in GPIO but not C++ code. I just want an example how to turn ON and OFF the GPIO using C++.
EDIT:
I use "sudo python programName.py to run a simple blinking LED using python language.
How to run C++ or the .cpp in terminal? Is it sudo Cplusplus programName.cpp?
Re: C++
OK, if you are on this level you have some work to do.GarryBoi wrote:Wow thanks you guys for the quick reply.
EDIT:
I use "sudo python programName.py to run a simple blinking LED using python language.
How to run C++ or the .cpp in terminal? Is it sudo Cplusplus programName.cpp?

C and C++ programs need to be compiled before you can run them, that is a big difference with a language like Python, that runs through an interpreter. My advice would be to read an introductory book in programming in C or C++, or try to find a decent on-line course or website to get you started with C or C++.
- DougieLawson
- Posts: 40794
- Joined: Sun Jun 16, 2013 11:19 pm
- Location: A small cave in deepest darkest Basingstoke, UK
- Contact: Website Twitter
Re: C++
Python is an interpreted language.
C/C++ are compiled languages.
Compile that with
gcc -o h hello.c
Run that with
./h
C/C++ are compiled languages.
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main (int argc, char *argv[])
{
printf ("Hello World \r\n");
}
gcc -o h hello.c
Run that with
./h
Any language using left-hand whitespace for syntax is ridiculous
Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.
Any requirement to use a crystal ball or mind reading will result in me ignoring your question.
Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.
Any requirement to use a crystal ball or mind reading will result in me ignoring your question.
Re: C++
I entered your example program using nano saving as hello.c, and then entered gcc -o h hello.c and got back the following:
hello.c: In function ‘main’:
hello.c:8:1: error: stray ‘\302’ in program
hello.c:8:1: error: stray ‘\240’ in program
I'm new to Linux and C. I'd be grateful for help in understanding the messages and in taking some corrective action.
hello.c: In function ‘main’:
hello.c:8:1: error: stray ‘\302’ in program
hello.c:8:1: error: stray ‘\240’ in program
I'm new to Linux and C. I'd be grateful for help in understanding the messages and in taking some corrective action.
Re: C++
small update:
Instead of copying and pasting the code I typed it in directly and got the following error:
Instead of copying and pasting the code I typed it in directly and got the following error:
Code: Select all
/usr/include/stdio.h:359:12: note: expected ‘const char * __restrict__’ but argument is of type ‘int’
Re: C++
Did you use the correct double-quotes " around the character string?
You'll get that sort of error if you use single quotes ' instead.
Code: Select all
printf ("Hello World \r\n");
Code: Select all
printf ('Hello World \r\n');
Re: C++
Final update:
I changed void main to int main and it appeared to compile, producing the Hello World message on typing ./h
Why it works now is a problem for me. If anyone can suggest a good tutorial on using C to help me make sense of all this I'd be most grateful.
I changed void main to int main and it appeared to compile, producing the Hello World message on typing ./h
Why it works now is a problem for me. If anyone can suggest a good tutorial on using C to help me make sense of all this I'd be most grateful.
Re: C++
@rpdom:
I did use double quotes but I think some of the errors occurred due to my copying and pasting introducing hidden characters maybe. I found a suggestion elsewhere on the internet to change void to int and it seemed to work - why I haven't a clue!
I did use double quotes but I think some of the errors occurred due to my copying and pasting introducing hidden characters maybe. I found a suggestion elsewhere on the internet to change void to int and it seemed to work - why I haven't a clue!
Re: C++
Yes, the copy paste method does sometimes introduce nasty charactersalechk wrote:@rpdom:
I did use double quotes but I think some of the errors occurred due to my copying and pasting introducing hidden characters maybe. I found a suggestion elsewhere on the internet to change void to int and it seemed to work - why I haven't a clue!

I'm not sure why changing main from void to int fixed the problem, although really main should return an integer status. Ideally you should put something like a return(0) at the end, I think.
I've tried the code myself, with both void and int and I can't get it to fail. Odd...
- FTrevorGowen
- Forum Moderator
- Posts: 6056
- Joined: Mon Mar 04, 2013 6:12 pm
- Location: Bristol, U.K.
- Contact: Website
Re: C++
Being somewhat "long in the tooth"** I'm unfamiliar with any "web-based" tutorial's but, when first working with any new programming language (or variant) I have found the "Sam's Teach yourself ..." series helpful. A quick search on Amazon suggests that such can be acquired (used) for a few pounds (or, maybe borrowed from a local library).alechk wrote:Final update:
I changed void main to int main and it appeared to compile, producing the Hello World message on typing ./h
Why it works now is a problem for me. If anyone can suggest a good tutorial on using C to help me make sense of all this I'd be most grateful.
Trev.
** having just passed 21 for the 3'rd. time

Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's & P400. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm
- DougieLawson
- Posts: 40794
- Joined: Sun Jun 16, 2013 11:19 pm
- Location: A small cave in deepest darkest Basingstoke, UK
- Contact: Website Twitter
Re: C++
That's due to a cut'n'paste error. You've introduced some wonky characters by editing things using Windows.
Any language using left-hand whitespace for syntax is ridiculous
Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.
Any requirement to use a crystal ball or mind reading will result in me ignoring your question.
Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.
Any requirement to use a crystal ball or mind reading will result in me ignoring your question.
Re: C++
I'm just struggling with C and C++ and Geany by my own.
here are some of my experiences and tortures, I hope you can read German or use Google translate successfully:
http://www.mindstormsforum.de/viewtopic ... 689#p67777 Geany IDE
http://www.mindstormsforum.de/viewtopic ... 689#p67773 wiringPi, pigpio installation
http://www.mindstormsforum.de/viewtopic ... 689#p67778 wiringPi, pigpio examples
http://www.mindstormsforum.de/viewtopic ... 689#p67771 ANSI C
http://www.mindstormsforum.de/viewtopic ... 689#p67772 C++
http://www.mindstormsforum.de/viewtopic.php?f=78&t=8689 TOP
but to be honest, it's absolutely frustrating...
here are some of my experiences and tortures, I hope you can read German or use Google translate successfully:
http://www.mindstormsforum.de/viewtopic ... 689#p67777 Geany IDE
http://www.mindstormsforum.de/viewtopic ... 689#p67773 wiringPi, pigpio installation
http://www.mindstormsforum.de/viewtopic ... 689#p67778 wiringPi, pigpio examples
http://www.mindstormsforum.de/viewtopic ... 689#p67771 ANSI C
http://www.mindstormsforum.de/viewtopic ... 689#p67772 C++
http://www.mindstormsforum.de/viewtopic.php?f=78&t=8689 TOP
but to be honest, it's absolutely frustrating...

#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(;;);}
#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(;;);}
Re: C++
That is not a valid signature for the main function in a C program. The only valid signatures for the main function in C99 are int main(void) or int main(int argc, char *argv[]) (or equivalent).DougieLawson wrote:Code: Select all
void main (int argc, char *argv[])
Code: Select all
#include <stdio.h>
int main(void)
{
printf("Hello World\n);
return 0;
}
- DougieLawson
- Posts: 40794
- Joined: Sun Jun 16, 2013 11:19 pm
- Location: A small cave in deepest darkest Basingstoke, UK
- Contact: Website Twitter
Re: C++
If my main function doesn't set a return code then it won't return an int, therefore void reflects exactly what the code is doing. I don't care about your stinking standards.
Any language using left-hand whitespace for syntax is ridiculous
Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.
Any requirement to use a crystal ball or mind reading will result in me ignoring your question.
Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.
Any requirement to use a crystal ball or mind reading will result in me ignoring your question.
- GerardWassink
- Posts: 103
- Joined: Sun Aug 02, 2015 5:57 pm
- Location: Ulrum (Gr), Netherlands
- Contact: Website
Re: C++
An entry level tutorial can easily be found I guess, as mentioned earlier in this thread. When I want to know something, I always return to the famous book "The C programming language", the original "C-Bible" by Kernigan and Ritchie.alechk wrote:Final update:
... If anyone can suggest a good tutorial on using C to help me make sense of all this I'd be most grateful.
HTH,
Gerard
---------ooooO----- \\\\\|/// -----Oooo--------
Hacker on ELF-II, ZX80/1, Commodore 64, 8080, x86,
IBM 370 family mainframes
Machine code! Assembly! C good second.
Running Pi's with Hercules and S/370 OS's
-------------oooO-----------Oooo-------------
Hacker on ELF-II, ZX80/1, Commodore 64, 8080, x86,
IBM 370 family mainframes
Machine code! Assembly! C good second.
Running Pi's with Hercules and S/370 OS's
-------------oooO-----------Oooo-------------
Re: C++
So have you've written your own C compiler to compile Lawson's-C ?DougieLawson wrote:. I don't care about your stinking standards.

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
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
Re: C++
IMO that's a quite OT discussion, not suitable for beginners - can you please continue your uppish, petty, and lecturing conversation by personal messages?
For a beginner there are different things which do come first, not such a rediculous nitpicking!
(edit:
wish yourself an int for Christmas and drop the void , period!)
For a beginner there are different things which do come first, not such a rediculous nitpicking!
(edit:
wish yourself an int for Christmas and drop the void , period!)
Last edited by davenull on Sun Oct 25, 2015 9:29 am, 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(;;);}
#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(;;);}
Re: C++
back to topic,
for general purposes about C and C++, to me always the cplusplus website was a real good helper, providing tutorials and language references and even feat. lots of code examples to either function, command, or expression:
http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/reference/clibrary/
HTH!
(admittedly this is NOT about compiler and makefile settings for the Linux gcc and g++ compilers though!)
for general purposes about C and C++, to me always the cplusplus website was a real good helper, providing tutorials and language references and even feat. lots of code examples to either function, command, or expression:
http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/reference/clibrary/
HTH!
(admittedly this is NOT about compiler and makefile settings for the Linux gcc and g++ compilers though!)
#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(;;);}
#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(;;);}
Re: C++
You think the importance of standards is off topic do you ? You'll waste a lot of time until you realise how wrong you are !davenull wrote:IMO that's a quite OT discussion, not suitable for beginners - can you please continue your uppish, petty, and lecturing conversation by personal messages?
For a beginner there are different things which do come first, not such a rediculous nitpicking!
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
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
Re: C++
K&R is still a very good book, especially if you already know another programming language. It might be difficult for a real beginner, which is why I would recommend two other books:GerardWassink wrote:An entry level tutorial can easily be found I guess, as mentioned earlier in this thread. When I want to know something, I always return to the famous book "The C programming language", the original "C-Bible" by Kernigan and Ritchie.alechk wrote:Final update:
... If anyone can suggest a good tutorial on using C to help me make sense of all this I'd be most grateful.
HTH,
Gerard
- C Programming - A Modern Approach, by K.N. King
- Programming in C - A complete introduction to the C programming language, by Stephen G. Kochan
Re: C++
Santa, as I said,
wish yourself an int for Christmas and drop the void , period!
And now keep on-topic, please, or send me (or either one) a PM!
(otherwise you destroy the flow of information)
wish yourself an int for Christmas and drop the void , period!
And now keep on-topic, please, or send me (or either one) a PM!
(otherwise you destroy the flow of information)
#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(;;);}
#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(;;);}