Heater
Posts: 13866
Joined: Tue Jul 17, 2012 3:02 pm

Re: Where can I begin learning C/C++?

Thu Oct 17, 2019 4:09 am

ejolson,
Is there any way to turn off all that ASCII art? It's filling my screen and causing the error messages to scroll off the top.
Yes, don't write any errors in your source code :)

I find that ASCII art very useful when the borrow checker is indicating what I incorrectly borrowed and from where. I don't often get masses of errors, probably because I chip away slowly, hitting "cargo check" after every couple of lines I add...

Turns out there is a github issue and discussion about your request:
https://github.com/rust-lang/rust/issues/27189

With some work arounds to trim down the number of errors for now.
Memory in C++ is a leaky abstraction .

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

Re: Where can I begin learning C/C++?

Thu Oct 17, 2019 4:11 am

ejolson wrote:
Thu Oct 17, 2019 3:06 am
Is there any way to turn off all that ASCII art? It's filling my screen and causing the error messages to scroll off the top.
Not the same, but you can reduce the amount of error message output from gcc:

Code: Select all

$ g++ -Werror -Wfatal-errors try.cpp
try.cpp: In function 'int main()':
try.cpp:5:12: error: no match for 'operator>>' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'const char [15]')
    5 |  std::cout >> "Hello, world!\n";
      |  ~~~~~~~~~ ^~ ~~~~~~~~~~~~~~~~~
      |       |       |
      |       |       const char [15]
      |       std::ostream {aka std::basic_ostream<char>}
compilation terminated due to -Wfatal-errors.
$ 

sal55
Posts: 63
Joined: Sat Sep 21, 2019 7:15 pm

Re: Where can I begin learning C/C++?

Thu Oct 17, 2019 10:51 am

jahboater wrote:
Thu Oct 17, 2019 4:11 am
ejolson wrote:
Thu Oct 17, 2019 3:06 am
Is there any way to turn off all that ASCII art? It's filling my screen and causing the error messages to scroll off the top.
Not the same, but you can reduce the amount of error message output from gcc:

Code: Select all

$ g++ -Werror -Wfatal-errors try.cpp
Yes, gcc takes the Build-Your-Own-Compiler approach by having to micro-manage every aspect of its behaviour via command line options, even down to refining the exact language you want to compile and whether some features should be allowed or not.

I've used gcc for C, and the output you get can be very unhelpful in terms of the quantity. There might be an actual error report but then hundreds of harmless warnings. But the error message, if any, has scrolled off the top of the screen. So did it compile successfully or not? Often the only hint is whether there is a delay after all the output, indicating that it is proceeding with the next stages rather than aborting, but there is no final message. You have to resort to scrolling the window, or capturing the output and scanning for errors, or capturing its return code, or even checking for whether a new executable exists!

So gcc itself is decidedly not beginner-friendly.

My own compilers are rather simple: there are no warnings, only hard errors. And they will show the first error and then stop. Then you can fix that one and try again (the compiler generates an info file that is picked up by my editor to take you straight to the error line in the right file). Since you have to fix one error at a time, why not show one error at a time? One error would often results in dozens of follow-up ones anyway if the compiler attempts to proceed.

We're not batch-compiling programs overnight anymore so that you have fix as much as possible to avoid wasting another day! Compilers, even gcc, are quick enough now, at least for the initial error-detecting phases, to just run them repeatedly. Quicker than perusing 18,000 lines of warnings anyway (which is what I've had on a 30,000 line input).

As for the error in my C++ example, that's not possible in any of my languages because it uses a sane means to do output ('print "Hello"), which is another obvious way of being more friendly. (Note that C++ can also make use of C's 'printf'-style functions to do output, also avoiding using the ugly std::cout and <<, but then you have to contend with 'format codes', which tells the compiler you are printing an integer, of a particular size and type, information it actually already knows.)

Heater
Posts: 13866
Joined: Tue Jul 17, 2012 3:02 pm

Re: Where can I begin learning C/C++?

Thu Oct 17, 2019 11:12 am

sal55,

Why do you insist on bashing on GCC for rather dubious reasons? GCC is a very fine C/C++ compiler.

Sure it has billions of options. I amost never use most of them, outside -Wall, 03, -o. It's great that they are there when you need them in some odd situation, otherwise the task would be impossible.

As shown above, modern versions have far better error messages.

If you are ignoring those hundreds of warnings you are doing the wrong thing. One should ensure there are no warnings. Fix them first! Trust me, they may look harmless but in the long run you will save a lot of bug hunting by not having them in the first place.

It's odd you should say GCC is not beginner friendly, thousands of Arduino users learn with it every day.
My own compilers ...
Ah, now I know why the bashing, you have created your own. Jolly good I say. What is it, where is it, can we use it? Does it produce code for x86, ARM, RISC V, and WASM? I need all of that from the same source just now.

I agree about the cout << thing. It's a damn ugly and verbose syntax, it's also amazingly slow to use if you need a lot of output. It should be taken out the back and shot.

prinf() takes a bit of getting used to for beginners, but it does a lot more than just "information it actually already knows already", like format things to your liking.
Memory in C++ is a leaky abstraction .

sal55
Posts: 63
Joined: Sat Sep 21, 2019 7:15 pm

Re: Where can I begin learning C/C++?

Thu Oct 17, 2019 12:41 pm

(Edited to remove off-topic stuff)
Heater wrote:
Thu Oct 17, 2019 11:12 am
Why do you insist on bashing on GCC for rather dubious reasons? GCC is a very fine C/C++ compiler.
Sure. It can produce some of the best code. It's available everywhere. It can do a million things.

But it's such a cumbersome product, the output can be so overwhelming , and the delay in producing a result so annoying when you have to do lots of compiles, that it is not my first choice when compiling C code for development. (I don't do C++).

ETA: My first choice is bcc then tcc and gcc on Windows, and tcc then gcc on Linux (bcc is my private Windows C compiler).
Last edited by sal55 on Thu Oct 17, 2019 4:27 pm, edited 1 time in total.

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

Re: Where can I begin learning C/C++?

Thu Oct 17, 2019 12:45 pm

sal55 wrote:
Thu Oct 17, 2019 10:51 am
My own compilers are rather simple: there are no warnings, only hard errors. And they will show the first error and then stop.
That's precisely what "-Werror -Wfatal-errors" does for GCC. -Werror makes all warnings into hard errors.
-Wfatal-errors abandons the compilation after the first error. You have the choice with GCC.
sal55 wrote:
Thu Oct 17, 2019 10:51 am
but then you have to contend with 'format codes', which tells the compiler you are printing an integer, of a particular size and type, information it actually already knows.)
No it does not already know.
Take a trivial "%5d" format. This prints the integer with a minimum field width of 5, right justified, padded with spaces.
How can the compiler possibly know all that ? And its a C language thing, nothing to do with GCC.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 24129
Joined: Sat Jul 30, 2011 7:41 pm

Re: Where can I begin learning C/C++?

Thu Oct 17, 2019 1:12 pm

What does all this have to do with the OP's question?

To save you typing, nothing.

Please keep on topic.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I think it’s wrong that only one company makes the game Monopoly.” – Steven Wright

Heater
Posts: 13866
Joined: Tue Jul 17, 2012 3:02 pm

Re: Where can I begin learning C/C++?

Thu Oct 17, 2019 9:52 pm

Does it matter? DOOMguy does not seem to be much interested in what anyone says here.

Perhaps those learning C++ would like to see the "Back to Basics" presentations from CppCon:

For example: “Back to Basics: Object-Oriented Programming” https://www.youtube.com/watch?v=32tDTD9UJCE

A search for "cppcon back to basics 2019" on YouTube will find all of them. Have a look at 2018 and 2017 as well.

Not for total beginners perhaps but the point of the suggestion is that they look at basic topics but making use of modern C++ facilities.
Memory in C++ is a leaky abstraction .

Floxi
Posts: 2
Joined: Fri Jul 05, 2019 11:45 am

Re: Where can I begin learning C/C++?

Tue Dec 03, 2019 3:24 pm

The already mentioned beginner's book by Simon Long got updated a while ago with a GUI section and I highly recommend it to absolute beginners. It's a really great start.
https://magpi.raspberrypi.org/books

Youtube videos are great, but it's extremely hard to look up stuff you might have missed or forgotten later, that's why I prefer books. (E.g. "C Primer Plus" or "C How to Program");
Even if you learn the basics from internet tutorials, "Understanding and Using C Pointers" is highly recommended as it does a terrific job at teaching the ins and outs of pointers.

As for the "Hello World" example: It's an absolute non-issue. Firstly, every half-decent IDE marks this specific error, and secondly, mixing up operators is a sign of lazy programming and can lead to horrific results which often only can get detected at runtime.

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

Re: Where can I begin learning C/C++?

Tue Dec 03, 2019 4:50 pm

Floxi wrote:
Tue Dec 03, 2019 3:24 pm
The already mentioned beginner's book by Simon Long got updated a while ago with a GUI section and I highly recommend it to absolute beginners. It's a really great start.
https://magpi.raspberrypi.org/books

Youtube videos are great, but it's extremely hard to look up stuff you might have missed or forgotten later, that's why I prefer books. (E.g. "C Primer Plus" or "C How to Program");
Even if you learn the basics from internet tutorials, "Understanding and Using C Pointers" is highly recommended as it does a terrific job at teaching the ins and outs of pointers.

As for the "Hello World" example: It's an absolute non-issue. Firstly, every half-decent IDE marks this specific error, and secondly, mixing up operators is a sign of lazy programming and can lead to horrific results which often only can get detected at runtime.
It is nice to see a new edition of that introductory C programming book. I still find it strange that the author persists in the error of writing

void main()

rather than the correct

int main()

In my opinion, it does not reflect well on the MagPi publishers that the author's insistence on intentionally making this error was not overridden by technical review and editor oversight. At the same time, I greatly appreciate a book that teaches introductory programming for true beginners using a language that wears as well with continued use as C does. Thank you!

One thing missing from the present thread on learning C is how important it is to choose a suitable programming project to motivate the learning. In my opinion, only by writing code is it possible to learn any kind of computer programming. Unfortunately, beginners often want to write a full symphony before creating a few tunes.

The reason for this may be a source of examples: While there are many songs made by beginning musicians that play guitar and sing, there are relatively few GitHub repositories that consist of fun programs written in C that are simple enough for a beginner to understand, improve and learn from. Moreover, those repositories that do exist are not popular, hummable or easy to find.
Last edited by ejolson on Tue Dec 03, 2019 5:13 pm, edited 3 times in total.

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

Re: Where can I begin learning C/C++?

Tue Dec 03, 2019 5:02 pm

ejolson wrote:
Tue Dec 03, 2019 4:50 pm
rather than the correct

int main()
Don't you need "int main( void )" to be correct?
ejolson wrote:
Tue Dec 03, 2019 4:50 pm
One thing missing from the present thread on learning C is how important it is to choose a suitable programming project to motivate the learning. In my opinion, only by writing code is it possible to learn any kind of computer programming. Unfortunately, beginners often want to write a full symphony before creating a few tunes.
Most definitely true.
And you must write original code yourself rather than blindly following examples.

DarkElvenAngel
Posts: 194
Joined: Tue Mar 20, 2018 9:53 pm

Re: Where can I begin learning C/C++?

Tue Dec 03, 2019 7:06 pm

jahboater wrote:
Tue Dec 03, 2019 5:02 pm
ejolson wrote:
Tue Dec 03, 2019 4:50 pm
rather than the correct

int main()
Don't you need "int main( void )" to be correct?
ejolson wrote:
Tue Dec 03, 2019 4:50 pm
One thing missing from the present thread on learning C is how important it is to choose a suitable programming project to motivate the learning. In my opinion, only by writing code is it possible to learn any kind of computer programming. Unfortunately, beginners often want to write a full symphony before creating a few tunes.
Most definitely true.
And you must write original code yourself rather than blindly following examples.
I think it would depend on what your trying to do. In my personal journey of learning I had the background from school long ago learning Turbo Pascal and basic and I never found satisfaction with it back then. Now I aim high and go for something challenging. I might not get it right away but when you crack it then you have something to be proud of. That's just my personal thoughts and it's not for everyone. I have a background in computers so it's not like starting right from scratch. Starting from scratch you have to do learn your basics

sal55
Posts: 63
Joined: Sat Sep 21, 2019 7:15 pm

Re: Where can I begin learning C/C++?

Tue Dec 03, 2019 8:20 pm

jahboater wrote:
Tue Dec 03, 2019 5:02 pm
Don't you need "int main( void )" to be correct?
Yes. There are 2-3 possible prototypes for 'main', and 'int main()' isn't one of them. The return value needs to be 'int' because the program is expected to return a status code. Although a compiler will warn if you pile on enough options, a beginner won't know about that and won't do it. The gcc examples in that C book don't use any such options.

One problem with main() is that you can then call it like this:

Code: Select all

 int main() {
	main(1,2,3,4,"five");
	main(main);
}
It's not a good idea to do this anyway (main calling main), but using (void) or (int nargs, char** args), it has a better chance of being reported.

The C/GUI book itself seems rather odd to me: a very lightweight introduction to C occupying half of an already slim volume, then it gets into GTK, one of the most complex GUI toolkits you can have (comprising some 600 C headers IIRC). Not the sort of sort of thing you'd want to do in C anyway.
Last edited by sal55 on Tue Dec 03, 2019 10:03 pm, edited 1 time in total.

Heater
Posts: 13866
Joined: Tue Jul 17, 2012 3:02 pm

Re: Where can I begin learning C/C++?

Tue Dec 03, 2019 10:02 pm

DarkElvenAngel,
Starting from scratch you have to do learn your basics
Ha, I read that all wrong the first time through.

Back in the day there was no Scratch with which to learn the basics. We had to start from scratch with BASIC.

Now a days I would not recommend starting from scratch and learning ones basics with either Scratch or BASIC.

If you see what I mean :)
Memory in C++ is a leaky abstraction .

DarkElvenAngel
Posts: 194
Joined: Tue Mar 20, 2018 9:53 pm

Re: Where can I begin learning C/C++?

Tue Dec 03, 2019 11:14 pm

Heater,

Well done, well done. I see what you did there.

I agree wholeheartedly but schools always seem to favor these odd languages over something more useful. I remember my computer science teacher teaching us Turing and LOGO who if anyone uses those these days. I thought when we got into Turbo Pascal that was something more useful. At least people still use Pascal.

Good for the OP looking to learn something a little more useful.

One piece of advice when you write your code fix all the warnings don't ignore them because your code works.

jcyr
Posts: 483
Joined: Sun Apr 23, 2017 1:31 pm
Location: Atlanta

Re: Where can I begin learning C/C++?

Wed Dec 04, 2019 1:33 am

DarkElvenAngel wrote:
Tue Dec 03, 2019 11:14 pm
I agree wholeheartedly but schools always seem to favor these odd languages over something more useful.
I understand many institutions are turning to Python these days.
It's um...uh...well it's kinda like...and it's got a bit of...

DarkElvenAngel
Posts: 194
Joined: Tue Mar 20, 2018 9:53 pm

Re: Where can I begin learning C/C++?

Wed Dec 04, 2019 2:20 am

jcyr wrote:
Wed Dec 04, 2019 1:33 am
DarkElvenAngel wrote:
Tue Dec 03, 2019 11:14 pm
I agree wholeheartedly but schools always seem to favor these odd languages over something more useful.
I understand many institutions are turning to Python these days.
At least Python is something useful, many things can be done with it. It's not dependent on any given platform and micro python even runs on embedded microcontrollers. So it's a good first step to teach as an introduction. It's been awhile since I've been in school so nice knowing they have finally picked something better.

LdB
Posts: 1311
Joined: Wed Dec 07, 2016 2:29 pm

Re: Where can I begin learning C/C++?

Wed Dec 04, 2019 3:07 am

Talk about Necro a thread and taking it for a walk

The OP asked a question about C/C++ back in Tue Oct 01, 2019 and hasn't been back since.

Now we are talking about Python :-)

People no-one cares especially the OP.

Jamesh you said all that needs to be said a couple of posts above ... lock the thread.

User avatar
Gavinmc42
Posts: 4020
Joined: Wed Aug 28, 2013 3:31 am

Re: Where can I begin learning C/C++?

Wed Dec 04, 2019 3:11 am

Micropython is an option for Buildroot on Pi's.
That makes it useful for small embedded Pi OS's.

My Uni teaches and helps staff/researchers with Python, R and Github.
I suppose that is the science equivalent of Word, Excel and Powerpoint?
It has sort of become an expectation to know Python around here.
C++ I would regard as CS and/or a full time programmers coding language.

What is the more useful language for coders and everyone else?
C and Python respectively?
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

LdB
Posts: 1311
Joined: Wed Dec 07, 2016 2:29 pm

Re: Where can I begin learning C/C++?

Wed Dec 04, 2019 3:23 am

You are posting in a C/C++ forum section .. and you ask the question :-)

Some of us don't want to wade thru this junk, can you ask it in an appropriate forum area .. General Programming discussion would probably be the go.

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

Re: Where can I begin learning C/C++?

Wed Dec 04, 2019 3:52 am

LdB wrote:
Wed Dec 04, 2019 3:07 am
People no-one cares especially the OP.

Jamesh you said all that needs to be said a couple of posts above ... lock the thread.
I'm not sure pestering the administrators to lock threads started by people who subsequently leave the forum is a good use of anyone's time. While apparent it is that you don't want to discuss how to learn the C programming language, I was quite happy to find out about the new edition of Simon's book.

LdB
Posts: 1311
Joined: Wed Dec 07, 2016 2:29 pm

Re: Where can I begin learning C/C++?

Wed Dec 04, 2019 6:51 am

The irony of you accusing me of wasting time :-)
Please explain what Python, Logo, Basic or half the other junk in the posts above has to do with learning C?
If you are posting on topic I have no issue and I wouldn't complain.
Now all we need is 20 posts about me complaining, that should really finish the thread off :-)

Again I simply echo Jamesh, this junk is annoying you read it because of the title but the discussion doesn't match the topic.
jamesh wrote:
Thu Oct 17, 2019 1:12 pm
What does all this have to do with the OP's question?

To save you typing, nothing.

Please keep on topic.

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

Re: Where can I begin learning C/C++?

Wed Dec 04, 2019 3:57 pm

LdB wrote:
Wed Dec 04, 2019 6:51 am
The irony of you accusing me of wasting time :-)
I just finished looking through Simon's book to see if it suggested any additional programming exercises and projects that could be done in C for a beginner. I found six blank pages at the end. Maybe a discussion of suitable exercises that could go with each chapter would make that book a better learning resource and help anyone reading this thread get started with C as well. In the end, such an effort could result in a companion book with problems at the correct level.

Does anyone know if there is already a source of introductory programming projects designed to be completed in C with a difficulty level suitable for a person reading Simon's book?

Return to “C/C++”