Author Topic: Good C programming books  (Read 9466 times)

0 Members and 1 Guest are viewing this topic.

Offline JwillisTopic starter

  • Super Contributor
  • ***
  • Posts: 1710
  • Country: ca
Good C programming books
« on: September 26, 2019, 11:18:05 pm »
Hey folks. I haven't done any programmming in C since the ATARI ST days (1985-1991)  and I lost or gave away all the resources I used then.Now I'm having a job wrapping my head around this Arduino because I've forgotten everything.
I've started again with a project I found on you tube  using the TLC594016-ChannelLED Driver.
 
But I want to add more chips and do more patterns. I managed to figure out the circuit schematic reading the programming text. but still having trouble relearning the rest.
Can you folks direct me to some decent books so I can relearn.I like books because I find un-printed PDFs hard to work with and some web sites hard to navigate .

 
« Last Edit: September 26, 2019, 11:20:51 pm by Jwillis »
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4032
  • Country: nz
Re: Good C programming books
« Reply #1 on: September 27, 2019, 01:07:53 am »
This doesn't seem to have anything much to do with C?

p.s. Arduino language is officially called "Wiring" and is a carefully-crafted subset of both C++ and Java and sketches are supposed to work in either. (the IDE automatically adds forward function declarations for C++, and a class wrapper for Java)

I suspect the Java compatibility of standard code isn't checked all that often these days. And of course user-written code often uses full C++ features.

 

Offline JwillisTopic starter

  • Super Contributor
  • ***
  • Posts: 1710
  • Country: ca
Re: Good C programming books
« Reply #2 on: September 27, 2019, 05:50:12 am »
That explains why it looks so strange. I don't know anything about Java. So What I'm looking for is a book specifically for Arduino coding then?
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Good C programming books
« Reply #3 on: September 27, 2019, 09:10:43 am »
Quote
Arduino language is officially called "Wiring" and is a carefully-crafted subset of both C++ and Java and sketches are supposed to work in either. (the IDE automatically adds forward function declarations for C++
This is wrong.  The Arduino Language is officially called "the Arduino Language" (1)

It is based on the Wiring Language defined as part of Hernando Barragán's Master's Thesis (2)

Neither the Arduino Language nor the Wiring Language contain any Java, although they were influenced by "The Processing Language" (3) (which is Java), and have similar abstractions, elimination of build complexities, and rejection of the standard libraries.


While they'd apparently like to believe otherwise, the Arduino and Wiring Languages are both C++, though without the C++ STL or even normal C++ libraries (depending on actual chip, actually.)  They do some trivial pre-processing to generate forward declarations, and then run the standard avr-gcc/avr-g++ compilers.  And they have their own set of "standard" library functions (although avr-libc and/or newlib-nano are present as well.) (4)


You can also include plain C/C++ files in your Arduino projects, and the IDE will handle them fine (just with less pre-processing.)  (And within the limits of the C++ libraries and features available from the particular compiler.)


A good C book is a fine place to start, if you want to understand Arduino code.

« Last Edit: September 27, 2019, 09:17:33 am by westfw »
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Good C programming books
« Reply #4 on: September 27, 2019, 09:27:42 am »
Quote
A good C book is a fine place to start, if you want to understand Arduino code.

I should add that "a good C++ book" is probably a TERRIBLE place to start, if your goal is to understand Arduino.  Most C++ books will immediately plunge you into features and library functions that just aren't there in Arduino (or shouldn't be used, even if they do exist.)
Even for C, the starting "Hello World" program won't work because you need to use "Serial.print()" instead of "printf()"  (But that's the big one for "beginning C", whereas for C++... I think you'd run into a lot more.
 
The following users thanked this post: Ian.M

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Good C programming books
« Reply #5 on: September 27, 2019, 10:02:19 am »
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14464
  • Country: fr
Re: Good C programming books
« Reply #6 on: September 27, 2019, 03:29:58 pm »
Just a note, something I kind of keep repeating. C is NOT a subset of C++. The subset of C++ that looks like C still has pretty different rules from C about many things, and I personally think using C++ to learn C is definitely a borked approach.

So actually naming the subset Arduino uses "Arduino" was a good idea, so that things don't get mixed up.

It's not just the printf() that won't work. Many C constructs are invalid in C++.
 
The following users thanked this post: Nominal Animal

Offline JwillisTopic starter

  • Super Contributor
  • ***
  • Posts: 1710
  • Country: ca
Re: Good C programming books
« Reply #7 on: September 27, 2019, 07:23:28 pm »
Any personal recommendations on the books I should check out?I have nothing right now and theirs just so many choices.
 

Offline MosherIV

  • Super Contributor
  • ***
  • Posts: 1530
  • Country: gb
Re: Good C programming books
« Reply #8 on: September 27, 2019, 08:32:00 pm »
If you want to learn C, the original book
"The C programming language"
by Kernighan and Ritchie.

As others have pointed out, it will not directly work on Arduino. Some tailoring required.

Personally, I have not seen ANY Arduino example code that even bearly resembles C++
It all looks like some sort of C.
« Last Edit: September 27, 2019, 08:34:53 pm by MosherIV »
 
The following users thanked this post: Jwillis

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Good C programming books
« Reply #9 on: September 27, 2019, 10:53:22 pm »
Quote
The subset of C++ that looks like C still has pretty different rules from C about many things ...
It's not just the printf() that won't work. Many C constructs are invalid in C++.
"Many"?  Care to mention a few that are significant, something that would bite a beginning programmer, and not in the "things you shouldn't do anyway" category?  I took a look at https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B and the biggest things that jumped out where "C++ won't let you assign (void*) pointers to type pointers without an explicit cast" (which is trivial) and assorted "reserved keywords"...

printf() and "cout << stuff" are both capable of working "just fine" in Arduino.  It's just that neither stdout nor cout is defined, by default.


Quote
So actually naming the subset Arduino uses "Arduino" was a good idea, so that things don't get mixed up.
Maybe.  As things are now, we get questions from "experienced" programmers looking for "more complete definitions of the Arduino Language" (which don't exist, because it's "see a C/C++ manual.)  If it was heavily documented as being C++, we'd probably get a similar number of complaints (from similar sources) about exceptions or some STL thing not working.
 

Offline Ampera

  • Super Contributor
  • ***
  • Posts: 2578
  • Country: us
    • Ampera's Forums
Re: Good C programming books
« Reply #10 on: September 28, 2019, 06:16:31 am »
Learning C as a means to learn C++ is excellent, learning C++ to learn C is not so much.

The book I used to learn C is https://www.amazon.com/dp/0321776410/
I've found it to explain the basic programming concepts of C in a fairly thorough and easy to understand way, at least enough to use before simply relying on online reference materials.

Ardunino programming is also a much different concept, as the standard library is completely different. That's the wonder of C, in that the actual library functions being used can change radically based off what system you're using, but the core syntax and language structures will remain mostly compliant to one of the various C standards. Keep in mind you should have your provided bowl of asterisk cereal for all the damn exceptions that exist.

I personally started programming using the Turbo C implementation, as I could more easily jump into the advanced parts of the C language while doing neat things on a DOS platform (like direct screen memory access and messing with BIOS/IVT functions). This may or may not be a good place for you to start too, for which I would suggest the OSDev wiki as a good resource on the various inner mechanisms of the IBM PC architecture.
I forget who I am sometimes, but then I remember that it's probably not worth remembering.
EEVBlog IRC Admin - Join us on irc.austnet.org #eevblog
 
The following users thanked this post: Jwillis

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4032
  • Country: nz
Re: Good C programming books
« Reply #11 on: September 28, 2019, 06:51:14 am »
Personally, I have not seen ANY Arduino example code that even bearly resembles C++
It all looks like some sort of C.

Code: [Select]
void loop() {
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  delay(1);
}

What the heck do you call Serial.println(sensorValue)? That can only be C++ (or Java), not C.
 

Offline Ampera

  • Super Contributor
  • ***
  • Posts: 2578
  • Country: us
    • Ampera's Forums
Re: Good C programming books
« Reply #12 on: September 28, 2019, 07:44:13 am »
couldn't you store the pointer to a function in a structure?
I forget who I am sometimes, but then I remember that it's probably not worth remembering.
EEVBlog IRC Admin - Join us on irc.austnet.org #eevblog
 

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 2416
  • Country: us
Re: Good C programming books
« Reply #13 on: September 28, 2019, 07:44:39 am »
Personally, I have not seen ANY Arduino example code that even bearly resembles C++
It all looks like some sort of C.

Here you go, I created this to make you happy. Even used main instead of setup/loop to make it look more normal (the default main() is declaried with a weak attribute, so you can replace it). to make it look more normal. I already had Pin() and Port() classes, so this was trivial.
Code: [Select]
// blink program for digispark attiny85

#include "pin.hpp"

int main() {               

  init(); // initialize hardware timers so we can use delay()
 
  // initialize output pins
  Pin led0(Port::B, 0, OUTPUT); // utility led on digispark attiny85 version B
  Pin led1(Port::B, 1, OUTPUT); // utility led on digispark attiny85 version A

  // Blink the builtin led pins. Whichever pin the led is attached to will be visible to the user.
  while(1) {
    led0.toggle();
    led1.toggle();
    delay(1000);
  }
}
 

Offline JwillisTopic starter

  • Super Contributor
  • ***
  • Posts: 1710
  • Country: ca
Re: Good C programming books
« Reply #14 on: September 28, 2019, 08:08:39 am »
Looks like I have some catching up to do.
 

Offline MosherIV

  • Super Contributor
  • ***
  • Posts: 1530
  • Country: gb
Re: Good C programming books
« Reply #15 on: September 28, 2019, 10:07:59 am »
Quote
Learning C as a means to learn C++ is excellent
I would dissagree.

I learnt C first. Then I had problems converting from fuctional programming to object orientated.
I would recommend learning C++ or Java first to learn OO first.
It is easier to comvert from OO to functional (functional is a subset of OO)

Quote
led1.toggle();
As Ampera said, this can be a call to a function pointer in a struct. The dot operator does not make it C++ !

Objects/classes make it C++
 

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 2416
  • Country: us
Re: Good C programming books
« Reply #16 on: September 28, 2019, 10:39:55 am »
Personally, I have not seen ANY Arduino example code that even bearly resembles C++
It all looks like some sort of C.

Here you go, I created this to make you happy. Even used main instead of setup/loop to make it look more normal (the default main() is declaried with a weak attribute, so you can replace it). to make it look more normal. I already had Pin() and Port() classes, so this was trivial.
Code: [Select]
// blink program for digispark attiny85

#include "pin.hpp"

int main() {               

  init(); // initialize hardware timers so we can use delay()
 
  // initialize output pins
  Pin led0(Port::B, 0, OUTPUT); // utility led on digispark attiny85 version B
  Pin led1(Port::B, 1, OUTPUT); // utility led on digispark attiny85 version A

  // Blink the builtin led pins. Whichever pin the led is attached to will be visible to the user.
  while(1) {
    led0.toggle();
    led1.toggle();
    delay(1000);
  }
}
Quote
led1.toggle();
As Ampera said, this can be a call to a function pointer in a struct. The dot operator does not make it C++ !

Objects/classes make it C++

So how do you explain away the scope resolution operator in the declaration of led0 and led1?
But I'll give you more since you seem to be in denial:
Code: [Select]
#ifndef pin_hpp
#define pin_hpp

#include <avr/io.h>
#include "port.hpp"

class Pin {
public:
Pin(volatile Port &port, uint8_t pin, uint8_t mode, uint8_t activeLevel = HIGH);
void mode(uint8_t mode, uint8_t activeLevel = HIGH);
void high();
void low();
bool active() const;
void toggle();

private:
volatile Port *mPort;
uint8_t mPinMask;
};

inline void Pin::mode(uint8_t mode, uint8_t activeLevel) {
((Port *)mPort)->mode(mPinMask, mode, activeLevel);
}

inline void Pin::high() {
((Port *)mPort)->high(mPinMask);
}

inline void Pin::low() {
((Port *)mPort)->low(mPinMask);
}

inline bool Pin::active() const {
return ((Port *)mPort)->active(mPinMask);
}

inline void Pin::toggle() {
((Port *)mPort)->toggle(mPinMask);
}

#endif
Code: [Select]
#include "pin.hpp"

Pin::Pin(volatile Port &port, uint8_t pin, uint8_t mode, uint8_t activeLevel) :
mPort(&port),
mPinMask(1 << pin)
{
((Port *)mPort)->mode(mPinMask, mode, activeLevel);
}

Yeah, I left out the documentation blocks, since I didn't want you to get bored before you got to the code.
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14464
  • Country: fr
Re: Good C programming books
« Reply #17 on: September 28, 2019, 02:50:58 pm »
Quote
led1.toggle();
As Ampera said, this can be a call to a function pointer in a struct. The dot operator does not make it C++ !

Yes.

This, on the other hand, can't be C:
"Pin led0(Port::B, 0, OUTPUT);"
so it's immediately obvious that's it's not C code, and that Pin is a C++ object.
 

Offline MosherIV

  • Super Contributor
  • ***
  • Posts: 1530
  • Country: gb
Re: Good C programming books
« Reply #18 on: September 28, 2019, 06:33:37 pm »
Ok, I conceed. The scope operator is definitely C++

However, I re-iterate my original statement, most examples of Arduino code is more procedural than object orientated.
They are very poor examples to teach people good object orientated coding.

I would not recommend using Arduino to learn C++
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14464
  • Country: fr
Re: Good C programming books
« Reply #19 on: September 28, 2019, 07:49:43 pm »
Ok, I conceed. The scope operator is definitely C++

However, I re-iterate my original statement, most examples of Arduino code is more procedural than object orientated.
They are very poor examples to teach people good object orientated coding.

I would not recommend using Arduino to learn C++

Oh, I agree with that. I'm just saying it's also an even poorer way of learning C. ;D

(The libraries themselves are pretty much all written as OO. Granted, basic classes, but still OO. But then yes, most of the example code you can find outside of libraries is poor crap, that is neither really proper C++ nor C. ;D )
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Good C programming books
« Reply #20 on: September 28, 2019, 10:30:45 pm »
Quote
Personally, I have not seen ANY Arduino example code that even bearly resembles C++

heh.  It depends on what you think C++ should look like.Apparently there is a whole class of C++ programmers who think that C++ programs should look much different than "C with Classes."  (Arduino definitely leans toward "C with Classes."  I mean, not a single use of STL in the whole thing!  And no templates at all!  Precious little in the way of operator overloading!  Horrors!) (significant amounts of function overloading, though.)


Interesting discussion here: https://softwareengineering.stackexchange.com/questions/48401/learning-c-properly-not-c-with-classes


I suspect that the people who think like this are not embedded programmers.  I find the example "sort" code posted at that link to be ... horrifying, and most "C++ for Microcontrollers" advice starts with "avoid the STL, since it's heavily dependent on dynamic allocation."


Quote
I would not recommend using Arduino to learn C++

Agreed.  Although, one of the "problems" learning any new language or paradigm is finding a project that is small enough to understand, and big enough (and scoped) to provide motivation.  "Convert THIS segment of the Arduino code to be more in the C++ style" would probably be a great "learning C++" exercise.  (Likewise: "modify this C++ library to work better in the Arduino environment.")

« Last Edit: September 28, 2019, 10:33:04 pm by westfw »
 

Offline Ampera

  • Super Contributor
  • ***
  • Posts: 2578
  • Country: us
    • Ampera's Forums
Re: Good C programming books
« Reply #21 on: September 29, 2019, 03:37:43 am »
I would dissagree.

I learnt C first. Then I had problems converting from fuctional programming to object orientated.
I would recommend learning C++ or Java first to learn OO first.
It is easier to comvert from OO to functional (functional is a subset of OO)

Then perhaps you're getting at it wrong. There's nothing forcing you in C++ to do object oriented much of anything. If anything it's C with some nicer standard libraries and then OOP if you want it.
This is how I started C++, and I, for the most part, treat it a lot like C still, just with some nicer quality of life features like maps and simpler memory management that C doesn't have.
I forget who I am sometimes, but then I remember that it's probably not worth remembering.
EEVBlog IRC Admin - Join us on irc.austnet.org #eevblog
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Good C programming books
« Reply #22 on: September 29, 2019, 08:25:25 am »
"I can write C code in quite a few other languages!"
 
The following users thanked this post: Ampera, dcarr, worsthorse

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 2416
  • Country: us
Re: Good C programming books
« Reply #23 on: September 29, 2019, 09:05:37 am »
I learned and used C before C++, but there wasn't a choice in my case. It was before C++ was created.

I also learned the VI editor before the computer mouse was a thing. It's still worth using as well, and the fact that you still don't have to have a mouse to use it is a good thing.
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14464
  • Country: fr
Re: Good C programming books
« Reply #24 on: September 29, 2019, 02:28:06 pm »
"I can write C code in quite a few other languages!"

Ouch, that was a great summary of the current state of things, I think. :-DD
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf