Programming in C

Ah.

I think the best plan of action from here - is probably to take some programs I've written in the Arduino IDE that I know work and do as I wish them to, and then rewrite them in plain C, get them to work. Then C++ without all the auto initialisation of the Arduino IDE.

For a successful career in embedded programming

Here, dhenry has missed the key demographic, and the raison d'être of the Arduino community.

Given that, as established earlier, the Arduino is programmed in C++ and that the transformation from a program using setup and loop to one using main is trivial, I think you're worrying too much. Any coding you do using the Arduino IDE is directly relevant to building your C++ experience - the functions you write would look the same whichever skeleton they use to get invoked. If you're going to be working with other platforms, you might want to get used to direct port access and bit twiddling, but even there, you can write your own wrapper functions (if they don't already exist) if they're fast enough.

In short, don't be so concerned about whether it's 'pure' C++ and get down to some coding instead :stuck_out_tongue:

jtw11:
Fungus, I don't follow the logic of your post? I don't intend on programming C++ on a PC, all on MCUs?

You were thinking of "adding C++ to your portfolio"...

Using Arduino-style C++ on a desktop would be as disastrous as using desktop-style C++ on an Arduino. You might want to qualify it in your resume.

Ah, Fungus - I see your point. Advertising oneself as being able to code C++ could lead to a rather awkward moment so to speak when sat down and asked to code a PC based C++ program, as opposed to a MCU program in C++.

I need to have a good look through now all the library functions that make up the digitalWrites, analogWrites, serial prints etc. Many thanks for all input thus far.

If you're going to be working with other platforms, you might want to get used to direct port access and bit twiddling, but even there, you can write your own wrapper functions (if they don't already exist) if they're fast enough.

This is what I want to achieve - to be able to program an AVR MCU using Atmel Studio for example, in the future - without any mention of Arduino. I almost feel bad saying that - given the platform introduced me so nicely :slight_smile:

wildbill:
Any coding you do using the Arduino IDE is directly relevant to building your C++ experience - the functions you write would look the same whichever skeleton they use to get invoked

The way I view it, "writing a program" is really more like "writing a custom library so that main() is small and neat".

When you say C: structure, are you referring to the structure of a typical C: drive on a PC?

It is about your ability to break down a complex task into well defined pieces so that when they are re-assembled, they work flawlessly together. That's where the money is, besides creativity.

dhenry:

When you say C: structure, are you referring to the structure of a typical C: drive on a PC?

It is about your ability to break down a complex task into well defined pieces so that when they are re-assembled, they work flawlessly together. That's where the money is, besides creativity.

Hmmm.... that requires creativity, too (often). :slight_smile:

It is about your ability to break down a complex task into well defined pieces so that when they are re-assembled, they work flawlessly together. That's where the money is, besides creativity.

That's exactly what I'm doing now - just documenting program flow charts, that define exactly what must happen, and when - saving on RAM using is high on the priority list with this project, and thus all terms are controlled as to when they refresh or are recalculated.

From the arduino.cc home page:

The microcontroller on the board is programmed using the Arduino programming language (based on Wiring) and the Arduino development environment (based on Processing).

Now, we all know that description by "Team Arduino" is a bit of stretch -- the "Arduno Language", if it exists, is a certain amount of IDE preprocessing of the .pde/.ino "sketch" files that convert certain non-C++ Arduino specific stuff into "real" C++ that the g++ compiler can actually compile.

But it should be made clear that when you program in the Arduino IDE, there are enough differences that mean the syntax and semantics depart from standard C++ (in particular, the the C/C++ preprocessor), and these can lead to unexpected (and sometimes inconsistent) results that can be confusing to anyone who has actually programmed in C/C++ before.

Perhaps calling the "Arduino Language" an attempt at a simplified "dialect" of C/C++ is a fairer description?

I just think of the differences as simply "broken", mostly. But the "team" will say that substituting the standard keyword "bool" with "boolean", and saving the user from having to declare their own function prototypes, and hiding compiler warnings and error messages from users so as to not upset them, are helpful to novice programmers. So all a matter of taste, I suppose. I myself actually program Arduinos without using the IDE, so I can stay in standard C++, and avoid all the non-standard Arduinoisms.

As to the question of learning C or C++ first, I'd recommend learning C first. It's a smaller, simpler and cleaner language, and in many ways more coherent than C++, which is (relatively) vast, complex and untidy. Once you've mastered C, it's a matter of learning C++ incrementally as a series of extensions the basic core of the C language. Almost all of C is necessary to program effectively, much of C++ is not.

In my opinion, the first and last book you'll need for learning C is "The C Programming Language" by Kernighan and Ritchie. Like the C language itself, the text is small, simple and clean. A classic. :slight_smile:

Just a note:

there are enough differences that mean the syntax and semantics depart from standard C++

definitely not, IMHO. A "non-stadard" syntax sketch would certainly not compile. As for the semantics, the Reference section documents the included libraries, while for everything else one can search "normal" (i.e. non-Arduino specific) online c++ documentation.

Well, C and C++ require either pre-declaration of functions or function prototypes, so any forward references would fail in a straightforward C/C++ environment.

pico:
But it should be made clear that when you program in the Arduino IDE, there are enough differences that mean the syntax and semantics depart from standard C++

What the Arduino web site refers to as "Arduino Language" is really the Arduino library: Arduino - Home

The programming language used is C++, I don't think there's any difference in syntax or semantics apart from "PROGMEM".

The IDE inserts the Arduino header files and generates function prototypes for you (so you don't have to worry about forward references), that doesn't really change anything about the programming language.

fungus:
The programming language used is C++, I don't think there's any difference in syntax or semantics apart from "PROGMEM".

As I mentioned, there are both syntactic and semantic differences, particularly with respect to C/C++ preprocessor (which is part of the C/C++ standard).

By semantic differences, I mean things that have the same syntax but different meaning or interpretation. For example, both "Arduino language" and C++ will compile this excerpt without a syntax error, but with very different results:

#define FOO 1

#if FOO
#include <mylib.h>
#else
#include <theirlib.h>
#endif

A trivial syntactic difference between "Arduino Language" and C++ is the replacement of "bool" with "boolean". (And what were they thinking, anyway?)

A less trivial (and more important) syntactic difference is

#include "file_with_arbitrary_extens.ion"

will preprocess and compile happily in C++, but not in "Arduino Language" as defined by the IDE preprocessor.

Finally, if "Team Arduino" want to call their implementation of the wiring lib a "programming language", more power to them. It's not what anyone I know would call a programming language, however.

But at the end of the day, whatever it is, "Arduino Language" != C++. As I said above, an attempt at a "simplified dialect" would be the most generous description I can think of.

#include >theirlib.h>

Getting that to compile would be a feat and a half in any system.

AWOL:

#include >theirlib.h>

Getting that to compile would be a feat and a half in any system.

You replied before I spotted it and could edit it! You are fast off the mark. :slight_smile:

Actually, that example would compile, because the #include >theirlib.h> line would be preprocessed out by the standard C/C++ preprocessor. I'm not sure what the IDE preprocessor would do with it, however. Which further demonstrates the point, I suppose.

Nice nerd arguments so far. I'm a hardware guy so my opinions are of questionable value in a topic like this. But I base my opinion (that arduino's are programmed in the C++ language) simply based on the fact that the IDE calls the gcc compiler to compile the sketch. The fact that there are things being done by the arduino preprocessor, while interesting and good to know and understand, doesn't change my opinion that the arduino does not use a programming language other then C++. It's not like the preprocessor is acting like a front end language converter that might say convert Fortran or Basic statements to C/C++ statements.

But I enjoy reading nerd arguments anyway, they are much better then political arguments. Keep them coming. :wink:

Lefty

retrolefty:
But I base my opinion (that arduino's are programmed in the C++ language) simply based on the fact that the IDE calls the gcc compiler to compile the sketch. The fact that there are things being done by the arduino preprocessor, while interesting and good to know and understand, doesn't change my opinion that the arduino does not use a programming language other then C++.

Then you might as well say that the Arduino Language is actually assembly language, since the g++ compiler in turn translates the C/C++ syntax into assembly (it actually generates a .S file during compilation, and the gnu assembler turns it into machine code).

retrolefty:
It's not like the preprocessor is acting like a front end language converter that might say convert Fortran or Basic statements to C/C++ statements.

This is pretty close to the mark, actually.

In my opinion, the first and last book you'll need for learning C is "The C Programming Language" by Kernighan and Ritchie. Like the C language itself, the text is small, simple and clean. A classic.

2nd that. I would add, however, that K&R is probably beyond the comprehension of most C programmers and 99.99% of the arduino community. Without years or programming, making mistakes and tracking down those nasty bugs, it is not possible to appreciate it fully.

what were they thinking, anyway?

I would add to that list the (ab)use of int.

As I said above, an attempt at a "simplified dialect" would be the most generous description I can think of.

It may be more complicated than that. Arduino is meant to be used by people who are new to mcu, and likely will not utilize the environment for complex tasks. As such, a simplified dialect (dumbed down version of a fully C-compliant implementation) likely is more productive for its intended audience / niche.

As I said keep them coming. My role in life here is just to be sure arduino beginners understand to use resistors with their leds and separate power source for their servos. :wink:

Lefty